In: Computer Science
IN PYTHON:
Write a program to study countries and their capitals. The program will store country:capital information and allow the user to quiz themselves.
These two functions are required for this assignment. You may add other functions that you deem appropriate:
- menu() This function, which displays all the user options to the screen, prompts the user for their choice and returns their choice. This function will verify user input and ALWAYS return a valid choice.
- addCapital(dict) This function accepts a dictionary as a parameter. This dictionary contains store country:capital pairs.
THE PROGRAM MUST:
- Add a country and it’s capital to the dictionary. The addCapital function is to be called to complete this task.
- The user can enter a country, and if that country is in the dictionary, it’s capital is printed, otherwise, an error message is printed.
- Print out the number of countries currently represented in the dictionary.
- Print out all the countries which are currently represented in the dictionary.
- Print out all of the countries and their capitals currently stored. If there are no capitals currently stored in the dictionary, a message is printed
- Exit the program
""" Python program country capital """ def addCaptial(country_capital): state = input("Enter Country: ") capital = input("Enter Capital: ") country_capital[state] = capital return country_capital def getCapital(country_capital): state = input("Enter Country: ") try: return country_capital[state] except KeyError: print("Error: Country not found") def printCountries(country_capital): for key in country_capital.keys(): print(key) def printCountriesAndCapitals(country_capital): for key, val in country_capital.items(): print(key,':',val) def menu(): print("1. Add") print("2. Find Capital") print("3. Number of Countries") print("4. All the Countries") print("5. All the Countries and their Capitals") print("6. Exit") choice = int(input("Enter choice: ")) return choice if __name__ == '__main__': country_capital = { 'Finland': 'Helsinki', 'France': 'Paris', 'Germany': 'Berlin' } while True: choice = menu() if choice == 1: addCaptial(country_capital) elif choice == 2: getCapital(country_capital) elif choice == 3: print("Number of countires", len(country_capital)) elif choice == 4: printCountries(country_capital) elif choice == 5: printCountriesAndCapitals(country_capital) elif choice == 6: exit() print()