Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write this program in Python for an \"infinite\" interactive loop to process a d

ID: 3601562 • Letter: W

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 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!

Explanation / Answer

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
modified = False
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
if k in d:
override = input("Do you want to override existing entry (y/n)?: ")
if overide == "y":
d[k] = v
modified = True
else:
d[k] = v
modified = True
elif (choice=="2"): # Show value for a key
k = input("Give the key: ")
# complete the rest
if k in d:
print(d[k])
else:
print(k + " does not exist")
elif (choice=="3"): # Delete an entry
k = input("Give the key to delete the entry: ")
# complete the rest
if k in d:
del d[k]
modified = True
else:
print(k + " does not exist")
elif (choice=="4"): # Save the file
print("Saving the file")
# complete the rest
file = open(filename,"w")
for k in d:
file.write(k + "," + d[k] + " ")
file.close()
modified = False
elif (choice=="5"): # Print the current dictionary
l = list(d.keys()) # get all the keys
l.sort() # sort them
# complete the rest
for k in l:
print(k + "-" + d[k])
elif (choice=="6"): # Quit
loop=False
# complete the rest
if modified:
shouldSave = input("Dictionary has been modifed. Do you want to save it (y/n)? ")
if shouldSave == "y":
file = open(filename,"w")
for k in d:
file.write(k + "," + d[k] + " ")
file.close()
else :
print("Incorrect choice")

if __name__ == '__main__':
main()

# copy pastable code link: https://paste.ee/p/1exJX