Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my game only printing one output when it should be printing

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading