Error implementing phone book using Python pickle

Asked 1 weeks ago, Updated 1 weeks ago, 0 views

[Program Design]

Separate the following functions into functions.

[Phonebook] Pickle Module


import pickle 

phoneDic = {
    "Kim Gap-soon": "** Preventing exposure of mobile phone numbers to protect personal information ***",
    "Anyone": "*** Preventing exposure of mobile phone numbers to protect personal information ***",
    "Kim Gap-dol": "** Preventing exposure of mobile phone numbers to protect personal information ***",
    "Who": "*** Preventing exposure of mobile phone numbers to protect personal information ***"
}

print(phoneDic)

f = open("phonebook.p", 'wb')
pickle.dump(phoneDic, f)
f.close()

Source Code

def readPhonebook(): #Read the list of phone numbers saved through the pickle function.
    import pickle
    f = open('phonebook.p', 'rb')
    phoneDic = pickle.load(f)
    f.close()
def writePhonebook(): # Write to phonebook file
    import pickle
    global phoneDic
    phoneDic = { "Kim Gap-soon": "** Preventing cell phone number exposure to protect personal information ****", "Anyone": "** Preventing cell phone number exposure to protect personal information ****", "Kim Gap-dol": "** Preventing cell phone number exposure to protect personal information ****"
    print(phoneDic)
    f = open('phonebook.p', 'wb')
    pickle.dump(phoneDic, f)
    f.close()
def appendPhonebook(who): #Enter and save the name
    import pickle
    with open('phonebook.p','wb') as fw:
        pickle.dump(','+'{'+who+' : xxx-xxxx-xxxx'+'}', fw)
        print(phoneDic)
def searchPhonebook(): #Find the name in the phone number list.
    import pickle
    f = open("phonebook.p", 'rb')
    phoneList = str(f.readlines())
    f.close()
    print(phoneList)

    phoneDic={}
    for phone in phoneList:
        nn = phone.split('') # Make a list.
        PhoneDic[nn[0] = nn[1] # Add to the dictionary.

    for key, value in phoneDic.items():
        print(key, value)

    while True:
        who = input("enter name :")
        number = phoneDic.get(who)
        if (number == None):
            print(who+' : not in the phone book.')
            appendPhonebook(who)
            break
        elif (who == '\"\"'):
            break
        else:
            print('phone number:' + number)
readPhonebook()
writePhonebook()
searchPhonebook()
Creation

and so

Traceback (most recent call last):
  File "C:/Users/hjdyd/OneDrive/Desktop/data/1.py", line 47, in <module>
    searchPhonebook()
  File "C:/Users/hjdyd/OneDrive/Desktop/data/1.py", line 29, in searchPhonebook
    PhoneDic[nn[0] = nn[1] # Add to the dictionary.
IndexError: list index out of range

This error occurs.

python

2022-09-20 14:30

2 Answers

The problem arises here.

phoneDic={}
for phone in phoneList:
    nn = phone.split('') # Make a list.
    phoneDic[nn[0] = nn[1] # Add to dictionary

I don't know what it is because I didn't post about phoneList, but if you check the error message, you can infer that nn has been converted to a list with one element by looking at the list index out of range, request that is out of range.

Please print out the nn and check it out.


2022-09-20 14:30

readPhonebook is not how you do it.

defreadPhonebook(): #Read the list of phone numbers saved through the pickle function.
    import pickle
    f = open('phonebook.p', 'rb')
    phoneDic = pickle.load(f)
    f.close()
    return phoneDic

Make the phoneDic a function that returns the phoneDic. So,

phoneDic = readPhonebook()

for key, value in phoneDic.items():
    print(key, value)

You can use it like this. Use this in searchPhonebook and open and parse all the codes.


2022-09-20 14:30

If you have any answers or tips


© 2022 pinfo. All rights reserved.