import json def menu(): selection = int(input("1-create file, 2-read file, 3-add to file, 4-quit ")) while selection!=1 and selection!=2 and selection!=3 and selection!=4: print("You made an invalid selection, please try again") selection = int(input("1-create file, 2-read file, 3-add to file, 4-quit ")) return selection def print_menu(): selection = int(input("1-rivers and countries, 2-rivers, 3-countries ")) while selection!=1 and selection!=2 and selection!=3: print("You made an invalid selection, please try again") selection = int(input("1-rivers and countries, 2-rivers, 3-countries ")) return selection def create(object): overwrite = input("You are about to create a new file, existing data will be overwritten (q to quit, any key to continue) ") if overwrite !="q": with open("rivers.json", "w") as write_file: json.dump(object, write_file, indent=4, sort_keys=True) print("rivers.json has been created") def append(new_item): with open("rivers.json", "r+") as add_file: riverDictionary = json.load(add_file) riverDictionary.update(new_item) add_file.seek(0) json.dump(riverDictionary, add_file, indent=4, sort_keys=True) print("Data has been added to rivers.json") def read(): try: with open("rivers.json") as read_file: riverDictionary = json.load(read_file) except FileNotFoundError: print("The file doesn't exist or cannot be found.") print("Please make a different menu selection") else: choice = print_menu() if choice==1: print_all(riverDictionary) elif choice==2: print_key(riverDictionary) else: print_value(riverDictionary) def print_all(object): print("-----------------Rivers and Countries--------------------------------") for key, value in object.items(): if type(value)==list: print(f"The {key.title()} river flows through: ") for v in value: print("\t\t\t\t",v) else: if value.lower()=="united states" or value.lower=="united kingdom": print(f"The {key.title()} river is in the {value.title()}") else: print(f"The {key.title()} river is in the country of {value.title()}") def print_key(object): print("-----------------Rivers-------------------------------") for key in rivers.keys(): print(key.title()) def print_value(object): print("------------------Countries-------------------------------") for value in rivers.values(): if type(value)==list: for v in value: print(v) else: print(value) def get_key(): river=input("Enter the name of the river ") return river def get_value(): multiple=input("Does the river flow through multiple countries (y for yes)? ") if multiple=="y" or multiple == "Y": country=[] new_country=input("Enter the name of a country the river flows through (q to quit)") while new_country !="q": country.append(new_country) new_country=input("Enter the name of a country the river flows through (q to quit)") else: country = input("Enter the name of the country the river is in ") return country # initial dictionary rivers={ "Danube":["Germany","Austria","Slovakia","Hungary", "Coratia", "Serbia","Romania","Bulgaria", "Moldova", "Ukraine"], "Mississippi":"United States", "Amazon":["Brazil","Bolivia","Peru","Ecuador","Colombia","Venezuela","Guyana", "Suriname", "French Guiana"] } # manually adding to the dictionary rivers["Boardman"]="United States" choice=menu() while choice != 4: if choice == 1: create(rivers) elif choice == 2: read() elif choice == 3: new_river=get_key() new_country=get_value() new_dictionary_entry={new_river:new_country} append(new_dictionary_entry) else: print("The option you selected is not available, please try again") choice=menu()