Create a Dictionaries - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Create a Dictionaries (/thread-7041.html) |
Create a Dictionaries - GalaxyCR - Dec-19-2017 Hey guys, so I found this code on the web. I tried to run it and it works. The only thing is when I restart the program, all the supposed added/deleted were not save. I have heard that you should use File Function but I'm not sure how to edit/delete word from file. Any help will be appreciated.P.S This code does not belong to me geek = {"404": "clueless. From the web error message 404, meaning page not found.", "Googling": "searching the Internet for background information on a person.", "Keyboard Plaque" : "the collection of debris found in computer keyboards.", "Link Rot" : "the process by which web page links become obsolete.", "Percussive Maintenance" : "the act of striking an electronic device to make it work.", "Uninstalled" : "being fired. Especially popular during the dot-bomb era."} choice = None while choice != "0": print( """ Geek Translator 0 - Quit 1 - Look Up a Geek Term 2 - Add a Geek Term 3 - Redefine a Geek Term 4 - Delete a Geek Term """ ) choice = input("Choice: ") print() # exit if choice == "0": print("Good-bye.") # get a definition elif choice == "1": term = input("What term do you want me to translate?: ") if term in geek: definition = geek[term] print("\n", term, "means", definition) else: print("\nSorry, I don't know", term) # add a term-definition pair elif choice == "2": term = input("What term do you want me to add?: ") if term not in geek: definition = input("\nWhat's the definition?: ") geek[term] = definition print("\n", term, "has been added.") else: print("\nThat term already exists! Try redefining it.") # redefine an existing term elif choice == "3": term = input("What term do you want me to redefine?: ") if term in geek: definition = input("What's the new definition?: ") geek[term] = definition print("\n", term, "has been redefined.") else: print("\nThat term doesn't exist! Try adding it.") # delete a term-definition pair elif choice == "4": term = input("What term do you want me to delete?: ") if term in geek: del geek[term] print("\nOkay, I deleted", term) else: print("\nI can't do that!", term, "doesn't exist in the dictionary.") # some unknown choice else: print("\nSorry, but", choice, "isn't a valid choice.") input("\n\nPress the enter key to exit.")The Output
RE: Create a Dictionaries - Larz60+ - Dec-19-2017 This could be written to be much easier and efficient to use, but save that for another day. add two new items to the choice list: 5. Save to json file 6. Load from json file Is this homework? RE: Create a Dictionaries - GalaxyCR - Dec-19-2017 Thanks. Not really a homework. This is more like my for my own knowledge. I'm new to python. RE: Create a Dictionaries - Larz60+ - Dec-19-2017 In that case, here you go. As I stated before, this is poorly written, and could be redone: import json geek = {"404": "clueless. From the web error message 404, meaning page not found.", "Googling": "searching the Internet for background information on a person.", "Keyboard Plaque": "the collection of debris found in computer keyboards.", "Link Rot": "the process by which web page links become obsolete.", "Percussive Maintenance": "the act of striking an electronic device to make it work.", "Uninstalled": "being fired. Especially popular during the dot-bomb era."} choice = None filename = None filechoice = None def get_filename(filename): if filename is not None: print('Do you want to use the file named {}? :'.format(filename)) filechoice = None validchoices = ['y', 'Y', 'n', 'N'] while filechoice not in validchoices: filechoice = input('Enter Y or N: ') if filechoice == 'n' or filechoice == 'N': filename = input("Please enter file name: ") else: filename = input("Please enter file name you wish to use: ") return filename while choice != "0": print( """ Geek Translator 0 - Quit 1 - Look Up a Geek Term 2 - Add a Geek Term 3 - Redefine a Geek Term 4 - Delete a Geek Term 5 - Save as json file 6 - Load from json file """ ) choice = input("Choice: ") print() # exit if choice == "0": print("Good-bye.") # get a definition elif choice == "1": term = input("What term do you want me to translate?: ") if term in geek: definition = geek[term] print("\n", term, "means", definition) else: print("\nSorry, I don't know", term) # add a term-definition pair elif choice == "2": term = input("What term do you want me to add?: ") if term not in geek: definition = input("\nWhat's the definition?: ") geek[term] = definition print("\n", term, "has been added.") else: print("\nThat term already exists! Try redefining it.") # redefine an existing term elif choice == "3": term = input("What term do you want me to redefine?: ") if term in geek: definition = input("What's the new definition?: ") geek[term] = definition print("\n", term, "has been redefined.") else: print("\nThat term doesn't exist! Try adding it.") # delete a term-definition pair elif choice == "4": term = input("What term do you want me to delete?: ") if term in geek: del geek[term] print("\nOkay, I deleted", term) else: print("\nI can't do that!", term, "doesn't exist in the dictionary.") # save to json file elif choice == "5": filename = get_filename(filename) with open(filename, 'w') as fout: json.dump(geek, fout) # load json file elif choice == "6": filename = get_filename(filename) with open(filename, 'r') as fin: geek = json.load(fin) # some unknown choice else: print("\nSorry, but", choice, "isn't a valid choice.") input("\n\nPress the enter key to exit.") |