Python Forum
Help with Dictionaries problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Dictionaries problem
#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


Messages In This Thread
Help with Dictionaries problem - by alex_0 - Mar-21-2021, 10:07 PM
RE: Help with Dictionaries problem - by Serafim - Mar-22-2021, 09:02 AM
RE: Help with Dictionaries problem - by alex_0 - Mar-22-2021, 08:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie to Python - Problem in accessing Dictionaries and List sambill 1 3,095 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