我正在编写一个不可能的测验游戏,它确实有效。我希望程序在var fails
等于3时退出。
相反,当您输入三次错误答案时,程序会循环而不是退出。
print("Welcome to impossible quiz")
print("")
print("You get 3 fails then you're out")
print("")
startcode = int(input("Enter 0 to continue: "))
fails = 0
if startcode != 0:
exit(1)
print("Welcome")
print("")
print("Level one")
L1ans = input("1+1= ")
while L1ans != "window":
print("incorect")
fails = fails + 1
L1ans = input("1+1= ")
if fails = 3:
exit(1)
答案 0 :(得分:1)
if fails == 3:
应该做的工作
答案 1 :(得分:0)
欢迎使用Stackoverflow!
我只能看到你的代码有1个问题,所以你几乎就在那里! 你的if语句需要有2个相等的符号。
if fails == 3:
exit(1)
答案 2 :(得分:0)
您的逻辑有点复杂,并且您有语法错误。试试这个:
fails = 0 # Set your failure flag
correct = False # Set your correct flag
while not correct: # if not correct loop
answer = input("1+1= ") # get the user's answer here
correct = answer == "window" # check for correctness
if not correct: # Handle incorrect case
print("Incorrect.")
fails += 1
if fails > 3: # quit if we've looped too much. > is better than == for this
exit()
print("Correct!")
请注意,这很容易封装在可以处理任何问题的类中:
def ask_question(question, answer, fails) {
correct = False # Set your correct flag
while not correct: # if not correct loop
answer = input(question) # get the user's answer here
correct = answer == answer # check for correctness
if not correct: # Handle incorrect case
print("Incorrect.")
fails += 1
if fails > 3: # quit if we've looped too much. > is better than == for this
exit()
print("Correct!")
return fails
}
fails = 0
fails = ask_question("1+1= ", "window", fails)
fails = ask_question("Red and earth is", "Macintosh", fails)