我是一名老师,我正在尝试编写一个简单的函数,将学生的电子邮件保存在字典中,以便在另一个程序中使用。我需要在多次执行中保存字典,因此我尝试使用shelve
它来保存它;但是,第二次运行该函数后,我收到一个 unpickling 错误,表示 pickle 数据已被截断。这是代码:
shelfFile = shelve.open('mydata')
studentEmails = shelfFile['studentEmails']
def inputEmails():
while True:
nameInput = input('Name: ')
if nameInput == '':
break
emailInput = input('Email: ')
if emailInput == '':
print('Email not entered. Please try again.')
continue
while True:
print('Is this information correct? [Y]es or [N]o')
print('Name: ' + nameInput)
print('Email: ' + emailInput)
correctChoice = input('[Y] or [N]: ').upper()
if correctChoice == 'Y':
studentEmails[nameInput] = emailInput
break
elif correctChoice == 'N':
print('Okay. Please input again.') …
Run Code Online (Sandbox Code Playgroud)