2

I am coding an impossible quiz game, and it doest work. I want the program to exit when the var fails is equal to 3.

Instead, when you enter a wrong answer three times, the program loops rather quitting.

    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)
2
  • Try if fails == 3: Commented Mar 11, 2016 at 0:59
  • 1
    That should produce a syntax error, right? Commented Mar 11, 2016 at 1:01

3 Answers 3

1
if fails == 3:

should do the job

Sign up to request clarification or add additional context in comments.

Comments

0

Your logic is a bit convoluted, and you have a syntax error. Try this:

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!")

Note that this is easily encapsulated in a class that can handle any question:

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)

Comments

0

Welcome to Stackoverflow!

I can only see 1 problem with your code, so your were nearly there! Your if statement needs to have 2 equal symbols.

    if fails == 3:
        exit(1)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.