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)
3 Answers 3
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?"))
1 Comment
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
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?"))
again ==isn't assignment, it's checking for equality.