好的,我正在创建一个记忆游戏。我已经开发出程序向用户询问删除了哪个单词的位置,并且已经成功开发了如果它们正确运行的部分。但是,我很难找到如何让它失败的用户,如果他们错了三次。这是我到目前为止所做的:
def q1():
qone + 1
print("\n"*2)
while qone <= 3:
question1 = input("Which word has been removed? ")
if question1 == removed:
print("\n"*1)
print("Correct")
print("\n"*2)
q2()
else:
print("Incorrect")
q1()
else:
print("You're all out of guesses!")
input("Press enter to return to the menu")
menu()
return
`
答案 0 :(得分:0)
我的方法是删除递归并简单地增加失败尝试的计数器。
def q1():
qone = 0
print("\n"*2)
while qone < 3:
question1 = input("Which word has been removed? ")
if question1 == removed:
print("\n"*1)
print("Correct")
print("\n"*2)
q2()
return
else:
print("Incorrect")
qone += 1
print("You're all out of guesses!")
input("Press enter to return to the menu")
menu()
return
答案 1 :(得分:0)
qone + 1
时,您需要将其分配给某些内容(所以也许qone += 1
)答案 2 :(得分:0)
你不应该让函数调用自己,使用range作为循环,如果用户得到的问题是正确的,请转到下一个问题,如果他们弄错了打印输出:
def q1(removed):
print("\n"*2)
for i in range(3):
question1 = input("Which word has been removed? ")
if question1 == removed:
print("\nCorrect")
return q2()
print("\n\nIncorrect")
input("You're all out of guesses!\nPress enter to return to the menu")
return menu()
如果用户有三个错误的猜测,循环将结束,您将点击"You're all out of guesses!\nPress enter to return to the menu"