嘿,我对python很陌生,但我正在尝试制作一个简单的注册系统。我希望在添加成员时能够进行功能更新,但我不知道如何更新。正如您将在下面的代码中看到的,一旦我在菜单选项2中添加完成员,当我返回并尝试菜单选项1时,我刚刚添加的成员将不会出现。如何使show_members函数重新加载更新后的词典?
除了我上面的问题,我们欢迎您就如何改进以下代码提出任何建议。是的,我知道它是非常不完整的,但它是在制品。
import sys
import shutil
import os
tmp = os.path.isfile("members.py.tmp")
if tmp == True:
os.remove("members.py.tmp")
shutil.copyfile("members.py", "members.py.tmp")
from members import members
def show_menu():
os.system("clear")
print "
","*" * 12, "MENU", "*" * 12
print "1. List members"
print "2. Add member"
print "3. Delete member"
print "99. Save"
print "0. Abort"
print "*" * 28, "
"
return input("Please make a selection: ")
def show_members(members):
for keys in members.keys():
os.system("clear")
print "
Names", " ", "Code"
print keys, " - ", members[keys]
def add_member(members):
os.system("clear")
name = raw_input("Please enter name: ")
code = raw_input("Please enter code: ")
members[name] = code
return members
#with open("foo.txt", "a") as f:
# f.write("new line
")
running = 1
while running:
selection = show_menu()
if selection == 1:
show_members(members)
print "
> " ,raw_input("Press enter to continue")
elif selection == 2:
add_member(members)
print "
> " ,raw_input("Press enter to continue")
elif selection == 99:
shutil.copyfile("members.py.tmp", "members.py")
elif selection == 0:
os.remove("members.py.tmp")
sys.exit("Program Aborted")
else:
os.system("clear")
print "That is not a valid option!"
print "
> " ,raw_input("Press enter to continue")