-1

The while loop is not working properly. The again variable will dictate whether or not the while loop will be executed again. If again = 1, then the while loop will be executed and the program will run again. If again =0, then it will not.

For some reason, again=1 always, so no matter what, while loop is always being executed. Does anyone notice an error in the code?

 score = 0
 loops = 0
 again = 1
 while (again != 0):
    import random
    real = random.randint(1,9)
    fake1 = random.randint(1,9)
    fake2 = random.randint(1,9)
    comb = random.randint(1,9)
    rep = 0
    guess = 0

    if (comb == 1 or comb == 2 or comb == 3):
        print(real, fake1, fake2)
        loops += 1
        guess = int(input("Choose between these numbers"))
        if (guess == real):
            score += 1
            print ("Congrats!")
        else:
            print ("Wrong, better luck next time!")
    if (comb == 4 or comb == 5 or comb == 6):
        print (fake1, fake2, real)
        loops += 1
        guess = int(input("Choose between these numbers"))
        if (guess == real):
            score += 1
            print ("Congrats!")
        else:
            print ("Wrong, better luck next time!")

    if (comb == 7 or comb == 8 or comb == 9):
        print (fake2, real, fake1)
        loops += 1
        guess = int(input("Choose between these numbers"))
        if (guess == real):
            score += 1
            print ("Congrats!")
        else:
            print ("Wrong, better luck next time!")
    again == int(input("Do you wanna go again?"))
    print(again)
1
  • 1
    again == isn't assignment, it's checking for equality. Commented Jun 22, 2017 at 0:45

3 Answers 3

1

You use a comparison operator while assigning a value into variable called again :

again == int(input("Do you wanna go again?"))

You must delete one of the equals signs:

again = int(input("Do you wanna go again?"))
Sign up to request clarification or add additional context in comments.

1 Comment

I feel dumb, such an easy fix. Thanks!
0
again == int(input("Do you wanna go again?"))

This is not going to do what you think, because == means it is checking if this statement is true. You want to have a single =.

1 Comment

Wow I can't believe I didn't see this. Such a simple fix! Thanks!
0

For this line:

again == int(input("Do you wanna go again?"))

The ‘==‘ is what you use for returning a Boolean of either True or false when comparing two values. For example:

print(2 > 1)
# output: True

The code should be corrected to:

again = int(input("Do you wanna go again?"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.