我对编码非常陌生,对于这个特定的代码,我迷路了。 该程序的目的是列出用户输入的名称列表,进行存储,然后在程序重新启动时重新加载。 该程序不会引发错误,但也不会在重新加载时存储任何内容。 我的问题是,为什么不保存列表? 任何帮助将不胜感激。
这是我尝试的代码:
<pre>
import os
import pickle
def display_title_bar():
os.system('cls')
print("\t*** Greeter - Hello old and new friends! ***")
#user choice
def get_user_choice():
print('1- list peopel we know: ')
print('2- add someone new: ')
print('3- enter q to exit')
return raw_input('please enter 1 2 or q :')
def show_name():
print(' here are the people we know\n')
for name in names:
print(name.title())
def get_new_name():
new_name=raw_input(' please enter name: ')
if new_name in names:
print(' we allready know %s' %new_name)# test for existing members.
else:
names.append(new_name)
print('%s has been added to the list\n'%new_name)
def load_names():
try:
file_object=open('names.pydata','rb')
pickle.dump( names, file_object)
file_object.close()
return names
except Exception as e:
print(e)
return[]
def quit():
try:
file_object =open('names.pydata','wb')
pickle.dump(names,file_object)
file_object.close()
print(' i will rmemeber the names')
except Exception as e:
print(e)
print(' sorry something went wrong')
#Setup a loop where users can choose
names= load_names()
choice=''
display_title_bar()
while choice !='q':
# Respond to the user's choice.
choice=get_user_choice()
display_title_bar()
if choice=='1':
show_name()
elif choice =='2':
get_new_name()
elif choice =='q':
quit()
print('thanks see you again soon')
else:
print(' i dont know what is this\n')
<code>