Reputation: 268
So I have an input that takes the user input and uses it to try and locate the right dictionary using the key. It is linked up to a .dat file which all works fine and locates the dictionary.
If I simply enter the location prior to running the program like this it is fine:
print(useraccounts[56443]['forename'])
But when I use the input to fill in the '56443' section it won't work (the part that says 'id_find')
id_find = input('Enter the unique student ID: ')
if os.path.exists('useraccounts.dat') == True:
with open('useraccounts.dat', 'rb') as x:
useraccounts = pickle.load(x)
print(useraccounts[56443]['forename'])
print('Student Found: ')
print('\nForename: ', (useraccounts[id_find]['forename']))
print('Second name: ', (useraccounts[id_find]['surname']))
print('DOB: ', (useraccounts[id_find]['dob']))
print('Gender: ', (useraccounts[id_find]['gender']))
print('Username: ', (useraccounts[id_find]['username']))
print('Password: ', (useraccounts[id_find]['password']))
print('Class: ', (useraccounts[id_find]['class']))
Here is what I get back. You can clearly see that it finds the dictionary where I inputted the key before running the program (George is the forename field) but doesn't work when it uses the input to find it.
Enter the unique student ID: 56443
George
Student Found:
Traceback (most recent call last):
File "/Users/admin/Documents/Homework/Computing/spelling bee George Taylor.py", line 335, in <module>
teacher_menu()
File "/Users/admin/Documents/Homework/Computing/spelling bee George Taylor.py", line 224, in teacher_menu
student_edit()
File "/Users/admin/Documents/Homework/Computing/spelling bee George Taylor.py", line 28, in student_edit
print('\nForename: ', (useraccounts[id_find]['forename']))
KeyError: '56443'
Thanks in advance.
Upvotes: 0
Views: 94
Reputation: 11039
id_find = input('Enter the unique student ID: ')
Change to:
id_find = int(input('Enter the unique student ID: '))
Upvotes: 0
Reputation: 56654
The dictionary is keyed by integer 56443, you are searching with the string "56443".
Upvotes: 3