1

Well, I am a beginner and my variable (guess) input doesn't work with the if statement:

When I put on the input numbers from 0 to 9, I want it prints out Right number!, else it print out the other message:

guess = input("Choose a number between 0-9: ")
if guess <=9 and guess >=0:
    print("Right number!")
else:
    print("The number you entered is out of range try again!")
martineau
  • 119,623
  • 25
  • 170
  • 301
  • That's because your `guess` variable is a string (if you type in `9`, `guess` will be `'9'`). Convert it to an integer: `guess = int(input("Choose a number between 0-9: "))` – Joe Patten Jan 12 '19 at 23:56

1 Answers1

0

The input() function returns a string, not a number (e. g. "127", not 127). You have to convert it to a number, e. g. with the help of int() function.

So instead of

guess = input("Choose a number between 0-9: ")

use

guess = int(input("Choose a number between 0-9: "))

to obtain an integer in guest variable, not a string.
Alternatively you may reach it in 2 statements - the first may be your original one, and the second will be a converting one:

guess = input("Choose a number between 0-9: ")
guess = int(guess)

Note:

Instead of

if guess <=9 and guess >=0:

you may write

if 0 <= guess <= 9:           # don't try it in other programming languages

or

if guess in range(10):        # 0 inclusive to 10 exclusive
MarianD
  • 13,096
  • 12
  • 42
  • 54