Question
Python This is the Question: Write a program that keeps names and email addresses in a dictionary as key-value pairs. The program should display a
Python This is the Question:
Write a program that keeps names and email addresses in a dictionary as key-value pairs. The program should display a menu that lets the user look up a persons email address, add a new name and email address, change an existing email address, and delete an existing name and email address. The program should pickle the dictionary and save it to a file when the user exits the program. Each time the program starts, it should retrieve the dictionary from the file and unpickle it.
This is my code:
# This is a program that help users to store the names and email addresses.
# Global constants for menu choices LOOK_UP = 1 ADD = 2 CHANGE = 3 DELETE = 4 QUIT = 5
# main function def main (): # Unpickle the Dictionary email_addr = unpickle()
# Initialize a variable for the users choice choice = 0
while choice != QUIT: # Get the users menu choice. choice = get_menu_choice( )
# The menus choice. if choice == LOOK_UP: look_up( email_addr) elif choice == ADD: add( email_addr) elif choice == CHANGE: change( email_addr) elif choice == DELETE: delete( email_addr)
# Save the file and pickle it pickle( email_addr)
# This function should retrieve from the dictionary and unpickle it. def unpickle( email_addr): # To indicate end of the file end_of_file = FALSE # Open the file input_file = open( 'email_addr.dat', 'rb')
# Read to the end of the file. while not end_of_file: try: # Unpickle the object email_addr = pickle.load( input_file) except EOFError: end_of_file = True
# Close the file input_file.close( )
# Return to the dictionary return email_addr
# This function should pickle the dictionary and save it to a file when the user exits the # program. def pickle( email_addr): # Open the file output_file = open( 'email_addr.dat', 'wb') #Write the data pickle.dump( email_addr, output_file) # Close the file output_file.close( )
# This function would help the read the menu choice from the user and display the menu. def get_menu_choice( ): print( ) print( 'Names and their email address') print( '-----------------------------------------') print( '1. Look up an email address') print( '2. Add a new address') print( '3. Change an address') print( '4. Delete an address') print( '5. Quit the program') print( )
# Read the users choice choice = int( input( 'Please enter your choice:') )
# The error message when user enter a wrong choice. while choice < LOOK_UP or choice > QUIT: choice = int( input( 'You had entered a validate choice, please enter again:') )
# Return the users choice. return choice # This is a function to look up the email address with a matching name def look_up( email_addr): # Prompt the users to enter a name name = input( 'Please enter a name:')
# Look up the name in the dictionary print( email_addr.get( name, 'Not Found.') )
# This function adds a new entry into the dictionary. def add(email_addr): # Get the name and the email address. name = input( 'Please enter the name:') email = input( 'Enter the email address:')
# If the name does not exist, add it. if name not in email_addr: email_addr[name] = email else: print( 'This name already exist!')
# This function help to change an existing entry. def change( email_addr): # Get a name to look up. name = input( 'Enter a name:')
# Write in the new data if name in email_addr: email = input('Please enter a new email address:') email_addr[name] = email else: print( 'The name is not found!')
# This function do the deletion of an existing entry. def delete( email_addr): # Prompt the user of a name name = input( 'Enter a name: ')
# If the name is found, then delete the entry. if name in email_addr: del email_addr[name] else: print( 'That name is not found!')
# Call the main function main()
CAN SOMEONE FIX THE ERROR FOR ME PLEASEEE
--------------------------------------------------------------------------- TypeError Traceback (most recent call last)in 130 131 # Call the main function --> 132 main() in main() 11 def main (): 12 # Unpickle the Dictionary ---> 13 email_addr = unpickle( ) 14 15 # Initialize a variable for the users choice TypeError: unpickle() missing 1 required positional argument: 'email_addr'
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started