I am new to python and am trying to teach myself how to program in python through the us of codecademy. But in my own time I decided to attempt to program a phone book using dictionaries and the pickle function i read about on this forum. But in experimenting with pickle to save my dictionary for the phone book itself the code kept raising an "EOFError" I have no idea what this is or how to solve it. So, if anyone is able to help me i would really appreciate it
#My Code
import pickle
PhoneBook = {}
reloaded = {}
with open("C:\\Programming\\Phone Book.txt", "rb") as inf: #Modify this dependent on where the file is located
reloaded = pickle.load(inf)
def help():
print "add_contact('name', 'number') - Adds a new contact to your phone book."
print "display_phone_book() - Displays the contents of your phone book."
print "display_contact('name') - Displays the resident of your phone book with the corrosponding name."
def add_contact(name, number):
PhoneBook [name] = number
with open("C:\\Programming\\Phone Book.txt", "wb") as outf: #Modify this dependent on where the file is located
pikle.dump(PhoneBook, outf)
def display_phone_book():
print PhoneBook
def display_contact(name):
print "%s's phone number is:" %(name)
print PhoneBook[name]
#Error's Code:
def load_eof(self):
raise EOFError
dispatch[''] = load_eof
NameErroron thatpikle.dump, or at least it would if you actually called any of these functions. See MCVE.pikle.dump(PhoneBook, outf)definitely won't work. What is the last function supposed to be doing? I'm very confused. Looks like you'd be setting the value of a key''in the dictionarydispatchequal to a function? Except you have no such dictionary, and none of your functions get called.PhoneBook.txt, even the very first time you run the program, when you've never saved anything there yet, that's obviously not going to work. To fix that, you probably want atry/exceptthat does some useful default behavior (maybe nothing) if the file is missing or empty.