Objective
Develop a Python program to create a database for storing information about urban areas.
In the first approach, we will store only the name of each urban area and the number of inhabitants, and allocate space for up to 500 entries.
The menu should include the following options:
1 .- Add a new urban area (at the end of the existing data)
2 .- View all urban areas (name and inhabitants)
3 .- Modify a record (rename and/or change the number of inhabitants)
4 .- Insert a new record (in a specified position, moving the following ones to the right)
5 .- Delete a record (moving the following ones to the left so that no empty spaces are left)
6 .- Search in the records (display the ones which contain a certain text in their name, whether in upper or lower case, using partial search)
7 .- Correct the capitalization of the names (turn into uppercase the first letter and the ones after a space, and make the rest lowercase)
0 .- Exit
Example Python Exercise
Show Python Code
# Initialize empty list to store urban areas data
urban_areas = []
# Start the program
while True:
# Display the menu
print("\nMenu:")
print("1. Add a new urban area")
print("2. View all urban areas")
print("3. Modify a record")
print("4. Insert a new record")
print("5. Delete a record")
print("6. Search in the records")
print("7. Correct the capitalization of the names")
print("0. Exit")
choice = input("Enter your choice: ")
if choice == '1':
# Add a new urban area
name = input("Enter the name of the urban area: ")
inhabitants = int(input("Enter the number of inhabitants: "))
urban_areas.append((name, inhabitants))
elif choice == '2':
# View all urban areas
if urban_areas:
for i, (name, inhabitants) in enumerate(urban_areas, start=1):
print(f"{i}. {name} - {inhabitants} inhabitants")
else:
print("No data available.")
elif choice == '3':
# Modify a record
index = int(input("Enter the record number to modify: ")) - 1
if 0 <= index < len(urban_areas):
name = input("Enter the new name of the urban area: ")
inhabitants = int(input("Enter the new number of inhabitants: "))
urban_areas[index] = (name, inhabitants)
else:
print("Invalid record number.")
elif choice == '4':
# Insert a new record at a specified position
index = int(input("Enter the position to insert at (1-based index): ")) - 1
if 0 <= index < len(urban_areas):
name = input("Enter the name of the urban area: ")
inhabitants = int(input("Enter the number of inhabitants: "))
urban_areas.insert(index, (name, inhabitants))
else:
print("Invalid position.")
elif choice == '5':
# Delete a record
index = int(input("Enter the record number to delete: ")) - 1
if 0 <= index < len(urban_areas):
urban_areas.pop(index)
print("Record deleted.")
else:
print("Invalid record number.")
elif choice == '6':
# Search in the records
search_text = input("Enter the text to search for: ").lower()
found = False
for name, inhabitants in urban_areas:
if search_text in name.lower():
print(f"{name} - {inhabitants} inhabitants")
found = True
if not found:
print("No matching records found.")
elif choice == '7':
# Correct the capitalization of the names
for i in range(len(urban_areas)):
name, inhabitants = urban_areas[i]
corrected_name = ' '.join([word.capitalize() for word in name.split()])
urban_areas[i] = (corrected_name, inhabitants)
print("Capitalization corrected.")
elif choice == '0':
# Exit the program
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")
Output
Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 1
Enter the name of the urban area: New York
Enter the number of inhabitants: 8419600
Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 1
Enter the name of the urban area: Los Angeles
Enter the number of inhabitants: 3980400
Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 2
1. New York - 8419600 inhabitants
2. Los Angeles - 3980400 inhabitants
Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 4
Enter the position to insert at (1-based index): 1
Enter the name of the urban area: Chicago
Enter the number of inhabitants: 2716000
Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 2
1. Chicago - 2716000 inhabitants
2. New York - 8419600 inhabitants
3. Los Angeles - 3980400 inhabitants
Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 6
Enter the text to search for: york
New York - 8419600 inhabitants
Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 7
Capitalization corrected.
Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 2
1. Chicago - 2716000 inhabitants
2. New York - 8419600 inhabitants
3. Los Angeles - 3980400 inhabitants
Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 0
Exiting the program.
Share this Python Exercise