需要解决不正确的猜测循环

时间:2015-10-07 21:02:35

标签: python

好的,我正在创建一个记忆游戏。我已经开发出程序向用户询问删除了哪个单词的位置,并且已经成功开发了如果它们正确运行的部分。但是,我很难找到如何让它失败的用户,如果他们错了三次。这是我到目前为止所做的:

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
`

3 个答案:

答案 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)

  1. 执行qone + 1时,您需要将其分配给某些内容(所以也许qone += 1
  2. while循环外的其他内容是什么?
  3. 你似乎有一个递归定义。仔细考虑将要进行的调用链以及基本情况应该是什么。一旦了解了这些内容,就可以更轻松地编写代码。另外,考虑一下你是否需要递归:在这种情况下,它看起来并不像你想要的那样。

答案 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"