Question: Write this program in Python for an infinite interactive loop to process a dictionary. The program starts by asking an input file name where the

Write this program in Python for an "infinite" interactive loop to process a dictionary. The program starts by asking an input file name where the dictionary entries are stored. This file will be assumed be present and to contain two items in each line as key & value pairs separated by a comma. The file could be possibly empty. The program reads the contents of this file into a dictionary, closes the file and presents user with the following "menu" choices in an "infinite" loop:

1. Add an entry

2. Show value for a key

3. Delete an entry

4. Save the file

5. Print the current dictionary

6. Quit

Complete rest of the program to process the dictionary interactively with the user. The following appropriate actions should be taken for each one of them and then the menu should be shown again unless the user quits. Add an entry: Ask the user for the key and value, and make an entry, if the key already exists then prompt the user whether to overwrite its value. Show value for a key: Ask the user for the key and show the corresponding value, point out if the key does not exist. Delete an entry: Ask the user for the key and delete the entry, point out if the key does not exist. Save the file: Save to the same file name in the same comma-separated format (do not worry about the order of entries), make sure to close the file. Print the current dictionary: Show all the key-value entries of the current dictionary on the screen (Optional: in the alphabetical order of the keys). Quit: End the program. If the dictionary has been modified since the last save, then prompt the user whether it should be saved before quitting.

Program so far I have is below, I just have to finish the area where it says #complete the rest in bold. Thank you.

def main(): d = {} # dictionary

# Read the file's contents in the dictionary d filename = input("Give the file name: ") file = open(filename,"r") for line in file: # read key and value key, value = line.split(",") # remove whitespaces before and after key = key.lstrip() key = key.rstrip() value = value.lstrip() value = value.rstrip() # insert entry in the dictionary d[key] = value file.close()

loop=True # continue looping if true

while (loop): print(" Enter one of the following menu choices:") print("1 Add an entry") print("2 Show value for a key") print("3 Delete an entry") print("4 Save the file") print("5 Print the current dictionary") print("6 Quit") choice = input("Choice 1-6: ") print(" ")

if (choice=="1"): # Add an entry k = input("Give the key: ") v = input("Give its value: ") # complete the rest elif (choice=="2"): # Show value for a key k = input("Give the key: ") # complete the rest elif (choice=="3"): # Delete an entry k = input("Give the key to delete the entry: ") # complete the rest elif (choice=="4"): # Save the file print("Saving the file") # complete the rest elif (choice=="5"): # Print the current dictionary l = list(d.keys()) # get all the keys l.sort() # sort them # complete the rest elif (choice=="6"): # Quit loop=False # complete the rest else : print("Incorrect choice")

Thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!