所以我有一个输入,它接受用户的输入,并使用它来尝试使用键定位正确的字典。它链接到一个.dat文件,该文件工作正常,可以定位字典。你知道吗
如果我只是在运行程序之前输入位置就可以了:
print(useraccounts[56443]['forename'])
但是,当我使用输入来填写“56443”部分时,它将不起作用(表示“id\u 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']))
这是我得到的。您可以清楚地看到,它找到了我在运行程序之前输入键的字典(George是forename字段),但是当它使用输入来查找它时,它不起作用。你知道吗
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'
提前谢谢。你知道吗
更改为:
字典由整数56443键入,您正在使用字符串“56443”进行搜索。你知道吗
相关问题 更多 >
编程相关推荐