Objectivo
Desarrollar un programa en Python para crear una base de datos para almacenar información sobre áreas urbanas.
En la primera aproximación, almacenaremos únicamente el nombre de cada área urbana y el número de habitantes, y asignaremos espacio para hasta 500 registros.
El menú debe incluir las siguientes opciones:
1.- Agregar una nueva área urbana (al final de los datos existentes)
2.- Ver todas las áreas urbanas (nombre y habitantes)
3.- Modificar un registro (renombrar y/o cambiar el número de habitantes)
4.- Insertar un nuevo registro (en una posición determinada, desplazando los siguientes hacia la derecha)
5.- Borrar un registro (desplazando los siguientes hacia la izquierda para que no queden espacios vacíos)
6.- Buscar en los registros (mostrar los que contengan un texto determinado en su nombre, ya sea en mayúsculas o minúsculas, mediante búsqueda parcial)
7.- Corregir la capitalización de los nombres (convertir en mayúsculas la primera letra y las que siguen a un espacio, y poner en minúsculas el resto)
0.- Salir
Ejemplo Ejercicio Python
Mostrar Código Python
urban_areas = []
while True:
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':
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':
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':
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':
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':
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_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':
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':
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.
Código de Ejemplo copiado
Comparte este ejercicios Python