import time
import random
import sys
tries = 1
def start():
global tries
tries = 1
global chest1
chest1 = random.sample(xrange(1, 20), 1)
chest1==str
global chest2
chest2 = random.sample(xrange(1, 20), 1)
chest2==str
global chest3
chest3 = random.sample(xrange(1, 20), 1)
chest3==str
print chest1
while chest1 == chest2 or chest1 == chest3 or chest2 == chest3:
start()
else:
print"Alright lets begin!"
game()
def defeat():
time.sleep(3)
end = raw_input("Would you like to start again(yes or no)?")
if end == "yes":
start()
if end == "no":
print"Goodbye!"
time.sleep(1);
print"Shutting Down"
time.sleep(2)
sys.exit()
else:
print"Please input a valid answer"
def game():
global chest1
global chest2
global chest3
print chest1, chest2, chest3
num = input("Choose a chest from 1-20!")
if num == chest1 or num == chest2 or num == chest3:
print "Well Done! Get another chest to move on to Sudden Death!"
else:
while (tries < 3):
global tries
print"Try Again"
tries = tries + 1
game()
else:
print "You've taken too many tries.. YOU DIE!"
defeat()
运行此代码时,我到达if语句:
if num == chest1 or num == chest2 or num == chest3:
print "Well Done! Get another chest to move on to Sudden Death!"
并将其中一个箱子与我的输入相匹配(我知道自从我打印它们后选择的数字)它跳转到else语句并说“#34;再试一次”#34;即使我将正确的输入与胸部相匹配。我已经在这几个小时......请帮助。谢谢!
答案 0 :(得分:1)
至少有几个问题:
chest1
,chest2
,chest3
分配一个列表。请改为尝试:
import time
import random
import sys
tries = 1
def start():
global tries
tries = 1
global chest1
chest1 = random.randint(1, 20)
global chest2
chest2 = random.randint(1, 20)
global chest3
chest3 = random.randint(1, 20)
while chest1 == chest2 or chest1 == chest3 or chest2 == chest3:
start()
else:
print("Alright lets begin!")
game()
def defeat():
time.sleep(3)
end = raw_input("Would you like to start again(yes or no)?")
if end == "yes":
start()
if end == "no":
print("Goodbye!")
time.sleep(1);
print("Shutting Down")
time.sleep(2)
sys.exit()
else:
print("Please input a valid answer")
def game():
global chest1
global chest2
global chest3
print(chest1, chest2, chest3)
num = int(raw_input("Choose a chest from 1-20!"))
if num == chest1 or num == chest2 or num == chest3:
print("Well Done! Get another chest to move on to Sudden Death!")
else:
while (tries < 3):
global tries
print("Try Again")
tries = tries + 1
game()
else:
print("You've taken too many tries.. YOU DIE!")
defeat()
编辑:通常也不赞成使用全局变量。我冒昧地重构你的代码而不使用它们:
import time, random, sys
def start():
chest1 = random.randint(1, 20)
chest2 = random.randint(1, 20)
chest3 = random.randint(1, 20)
tries = 1
while chest1 == chest2 or chest1 == chest3 or chest2 == chest3:
start()
else:
print("Alright lets begin!")
game(chest1,chest2,chest3,tries=tries)
return tries
def defeat():
time.sleep(3)
end = raw_input("Would you like to start again(yes or no)?")
if end == "yes":
tries = start()
if end == "no":
print("Goodbye!")
print("Shutting Down")
sys.exit()
else:
print("Please input a valid answer")
def game(chest1,chest2,chest3,tries=None):
print(chest1, chest2, chest3)
num = int(raw_input("Choose a chest from 1-20!"))
if num == chest1 or num == chest2 or num == chest3:
print("Well Done! Get another chest to move on to Sudden Death!")
while (tries < 3):
tries += 1
game(chest1,chest2,chest3,tries=tries)
else:
print("Moving to Sudden Death!")
sudden_death(chest1,chest2,chest3,tries=tries)
else:
while (tries < 3):
print("Try Again")
tries += 1
game(chest1,chest2,chest3,tries=tries)
else:
print("You've taken too many tries.. YOU DIE!")
defeat()
def sudden_death(chest1,chest2,chest3,tries=None):
print("Sudden Death not yet implemented; exiting")
defeat()