Python Forum
Help with Dictionaries problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Dictionaries problem
#1
I have a homework assignment that uses dictionaries and I could use some help on it.

Here is the question I have which uses Harry Potter character names:

https://pastebin.com/1c7gme3L


There are two things I have an issues with in my code that I am unsure of how to fix.

1) Enter name:[email protected] is not on the correct line and needs to be moved up one line
https://imgur.com/a/AU4LLPk
I tried removing some of the \n newlines but this didn't fix the issue.

2) The output that I have is incorrect
https://imgur.com/a/c78OREI

In the correct example, the input name is Albus Dumbledore and the names in the output are in alphabetical order. In my output, the names are not in alphabetical order.

Any help on this is greatly appreciated.

Here is my code:

I think I need to fix the sorted dictionary that is near the bottom of the code.
I create the sorted dictionary, but when I write to the file I don't think I use this and write from the unsorted phoneBook dictionary instead. I am not sure exactly how to fix this, however.

# displays the menu
def menu():
   print('Enter')
   print('1) look up an email address')
   print('2) add a new name and email address')
   print('3) change an email address')
   print('4) delete a name and email address')
   print('5) save address book and exit:')


# phonebook dictionary
phoneBook = {}

# open the file
with open('phonebook.in') as f:
   lines = f.readlines()

   for i in range(0, len(lines), 2):
       # add to dictionary
       phoneBook[lines[i].rstrip('\n')] = lines[i+1].rstrip('\n')

# display the menu repeatedly
choice = ''
while choice != '5':
   menu()
   choice = input('')
   if choice == '1':
       name = input('Enter name:')
       if name in phoneBook.keys():
           print(phoneBook[name])
       else:
           print('Sorry, no contact exists under that name.')
   elif choice == '2':
       name = input('Enter name:')
       email = input('Enter the email:')
       if name in phoneBook.keys():
           print('Name already exists')
       else:
           phoneBook[name] = email
           print('Added successfully')
   elif choice == '3':
       name = input('Enter name:')
       if name in phoneBook.keys():
           email = input('Enter a new email address:')
           phoneBook[name] = email
           print('Email changed successfully')
       else:
           print('Sorry, no contact exists under that name.')
   elif choice == '4':
       name = input('Enter name:')
       if name in phoneBook.keys():
           del phoneBook[name]
           print('Deleted successfully')
       else:
           print('Sorry, no contact exists under that name.')
   elif choice == '5':
       sorted = {k : phoneBook[k] for k in sorted(phoneBook)}
       # open file to write
       f_out = open("phonebook.out", "w")
       for rec in phoneBook.keys():
           f_out.write(rec + '\n')
           f_out.write(phoneBook[rec] + '\n')
       f_out.close()
   else:
       print('Invalid Choice')
If I do this, then the only output I get is for Harry Potter.

   elif choice == '5':
       sorted = {k : phoneBook[k] for k in sorted(phoneBook)}
       # open file to write
       f_out = open("phonebook.out", "w")
       for rec in phoneBook.keys():
           f_out.write(rec + '\n')
           f_out.writesorte(sorted(phoneBook[rec] + '\n'))
       f_out.close()
   else:
       print('Invalid Choice')
Reply
#2
I haven't really understood error 1) unless you want the last line in the menu
to be printed without a newline but I am not certain that that is your problem.
The second error is easier to explain.
You are sorting the dictionary "phonebook" when you write it to the file but a dictionary is not possible to sort, it is by default unsorted, so it has no effect or at least it doesn't have the effect you expect. To write it in a sorted way, sort just the keys:
for rec in sorted(phonebook.keys()):
and delete the line
sorted = {k : phoneBook[k] for k in sorted(phoneBook)}
The function call
sorted(phoneBook)
gives you a sorted list with the keys so
for rec in sorted(phonebook):
would work as well.
Reply
#3
(Mar-22-2021, 09:02 AM)Serafim Wrote: I haven't really understood error 1) unless you want the last line in the menu
to be printed without a newline but I am not certain that that is your problem.
The second error is easier to explain.
You are sorting the dictionary "phonebook" when you write it to the file but a dictionary is not possible to sort, it is by default unsorted, so it has no effect or at least it doesn't have the effect you expect. To write it in a sorted way, sort just the keys:
for rec in sorted(phonebook.keys()):
and delete the line
sorted = {k : phoneBook[k] for k in sorted(phoneBook)}
The function call
sorted(phoneBook)
gives you a sorted list with the keys so
for rec in sorted(phonebook):
would work as well.


Thank you very much for your help with my issues. I was able to figure it out.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie to Python - Problem in accessing Dictionaries and List sambill 1 3,055 Aug-17-2017, 07:38 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020