我前几天制作了这段代码。除菜单选择外,它工作正常。 每当我测试它时,无论我为菜单做什么选项,都会重置密码!
所以,如果我插入'我做一个新游戏!'它将以'INSERT PASSWORD:'响应,开始重置密码序列。
我的代码如下:
import pickle
import time
# Beginning of program.
print ("Welcome to The Maze!")
time.sleep(2)
print ("Please enter your password to continue, or create a new one.")
has_pass = "false"
pass_correct = "false"
pass_input = "null"
password = "null"
with open('save_pass.dat', 'r') as sp:
password, has_pass = pickle.load(sp)
if has_pass=="true":
while pass_correct=="false":
pass_input = raw_input("INSERT PASSWORD: ")
if pass_input==password:
pass_correct = "true"
print ("Password Correct")
elif has_pass=="false":
password = raw_input("CREATE A NEW PASSWORD: ")
has_pass = "true"
with open('save_pass.dat', 'w') as sp:
pickle.dump([password, has_pass], sp, protocol=2)
# Defining loadGame()
def loadGame():
pass
# Defining newGame()
def newGame():
print("Are you sure? You will overwrite any existing data.")
reset = raw_input()
if "no" or "No" in reset:
menu()
if "yes" or "Yes" in reset:
level = 0
points = 0
with open('level_save.dat', 'w') as ls:
pickle.dump([level, points], ls, protocol=2)
loadGame()
# Defining menu()
def menu():
print 10 * "~" , "Menu" , 10 * "~"
print("1. New game\n2. Load previous game\n3. Reset password\n4. Exit")
print("~" * 26)
time.sleep(6)
menu_select = raw_input("Please choose an option: ")
在这里工作得很好......
if "3" or "reset" or "Reset" in menu_select:
with open('save_pass.dat', 'r') as sp:
password, has_pass = pickle.load(sp)
pass_correct = "false"
if has_pass=="true":
while pass_correct=="false":
pass_input = raw_input("INSERT PASSWORD: ")
if pass_input==password:
pass_correct = "true"
print ("Password Correct")
password = raw_input("CREATE A NEW PASSWORD: ")
has_pass = "true"
with open('save_pass.dat', 'w') as f:
pickle.dump([password, has_pass], f, protocol=2)
time.sleep(2)
print("Password succesfully saved!")
time.sleep(5)
menu()
if "4" or "exit" or "Exit" or "quit" or "Quit" in menu_select:
exit()
quit()
if "1" or "Newgame" or "newgame" or "new" or "New" in menu_select:
newGame()
menu()
有人可以帮忙吗?我尝试了很多解决方案但没有任何效果。