In: Computer Science
Complete the PA7_incomplete.py program (posted below) 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 and value pairs separated by a comma, like the example.txt file (posted below). 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: Add an entry Show value for a key Delete an entry Save the file Print the current dictionary 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.
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("\n\nEnter 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("\n\n")
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")
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 value entries of the current dictionary on the screen (in alphabetical order of the keys). End the program. If the dictionary has been modified since the last save, then prompt the user whether it should be saved before quitting.
CODE -
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("\n\nEnter 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("\n\n")
if (choice=="1"): # Add an entry
k = input("Give the key: ")
v = input("Give its value: ")
d[k] = v # Adding the entry to the dictionary
modified = True # Setting modified to true which indicates that dictionary has been modified after saving the file.
elif (choice=="2"): # Show value for a key
k = input("Give the key: ")
if k in d:
print("Key:", k, "\nValue:", d[k]) # Displaying the key and value if key is found in the dictionary
else:
print("Key not found!") # Displaying message that key not found if key is not present in the dictionary
elif (choice=="3"): # Delete an entry
k = input("Give the key to delete the entry: ")
if k in d:
del d[k] # Deleting the entry if key is found in the dictionary
modified = True # Setting modified to true which indicates that dictionary has been modified after saving the file.
print("Entry Deleted.")
else:
print("Key not present!") # Displaying message that key not present if key is not found in the dictionary
elif (choice=="4"): # Save the file
print("Saving the file")
file = open(filename,"w") # Opening the file for writing
for k in d:
file.write(k + ", " + d[k] + "\n") # Writing the key and value to the file seperated by comma
modified = False # Setting modified to false which indicates that dictionary has not been modified after saving the file.
file.close() # Closing the file
elif (choice=="5"): # Print the current dictionary
l = list(d.keys()) # get all the keys
l.sort() # sort them
for k in l:
print(k + " : " + d[k]) # Printing the keys and values of dictionary in order of keys appearing in the sorted list.
elif (choice=="6"): # Quit
loop=False
if modified: # Checking if dictionary has been modified after saving the file
choice = input("Do you want to save the file before exiting? (Y for yes and N for no) ") # Asking user whether to save file before exiting
if choice == "Y" or choice == "y":
print("Saving the file")
file = open(filename,"w") # Opening the file for writing
for k in d:
file.write(k + ", " + d[k] + "\n") # Writing the key and value to the file seperated by comma
file.close() # Closing the file
print("Exiting the program!\n\n")
else :
print("Incorrect choice")
main()
SCREENSHOTS -
CODE -
OUTPUT -
If you have any doubt regarding the solution, then do comment.
Do upvote.