0

Ok, I am creating a memory game. I have developed where the programme asks the user what word was removed, and have successfully developed the part that moves on if they get it right. However, I am struggling to find how to get it to only fail the user if they get it wrong three times. Here's what I have so far:

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 Answers3

0

My approach is to remove recursion and simply increase the counter of failed tries.

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
grael
  • 657
  • 2
  • 11
  • 26
0
  1. When you do qone + 1, you need to assign it to something (so perhaps qone += 1)
  2. What is the else outside the while loop linked to?
  3. You seem to have a recursive definition going. Think carefully about the chain of calls that would be made and what your base case should be. Once you know these things, it would be easier for you to write the code. Also, think about whether you need recursion at all: in this case, it doesn't seem like you would.
idjaw
  • 25,487
  • 7
  • 64
  • 83
sgrg
  • 1,210
  • 9
  • 15
0

You should not have the function calling itself, use range for the loop, if the user gets the question correct go to the next question, if they get it wrong print the output:

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()

If the user has three bad guesses the loop will end and you will hit the "You're all out of guesses!\nPress enter to return to the menu"

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321