Why is my game only printing one output when it should be printing
Question:
I am making a guess the number game, however in my if/else statement it is not printing the else instead it is printing the if always regardless of what input I give it. This is my code:
print("Welcome to the number game. In this game you are given a list of numbers from one to ten and you have to guess the right numbers!")
while 2 == 2:
guess = int(input("Choose a number from: 1,2,3,4,5,6,7,8,9!"))
if guess == 1 or 2 or 5 or 9:
print("You guessed a winning number! Congrats!")
else:
print("Maybe next time...")
Please explain to me why it is only printing "You guessed a winning number! Congrats!".
Help would be greatly appreciated.
Answers:
Because you wrote
if guess == 1 or 2 or 5 or 9:
It will test if "guess == 1" then it will test "if 2", not "if guess == 2", just "2" and and so on.
So you should write
if guess == 1 or guess == 2 or guess == 5 or guess == 9:
I recommend that you use lists :
winning_numbers = [1, 2, 5, 9]
if guess in winning_numbers:
#code goes here
I am making a guess the number game, however in my if/else statement it is not printing the else instead it is printing the if always regardless of what input I give it. This is my code:
print("Welcome to the number game. In this game you are given a list of numbers from one to ten and you have to guess the right numbers!")
while 2 == 2:
guess = int(input("Choose a number from: 1,2,3,4,5,6,7,8,9!"))
if guess == 1 or 2 or 5 or 9:
print("You guessed a winning number! Congrats!")
else:
print("Maybe next time...")
Please explain to me why it is only printing "You guessed a winning number! Congrats!".
Help would be greatly appreciated.
Because you wrote
if guess == 1 or 2 or 5 or 9:
It will test if "guess == 1" then it will test "if 2", not "if guess == 2", just "2" and and so on.
So you should write
if guess == 1 or guess == 2 or guess == 5 or guess == 9:
I recommend that you use lists :
winning_numbers = [1, 2, 5, 9]
if guess in winning_numbers:
#code goes here