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.
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')