0

Let's say I want the user to input a number between 1 and 50, so I do:

num = input("Choose a number between 1 and 50: ")

But if the user inputs a number that does not fall between 1 and 50, i want it to print:

Choose a number between 1 and 50: invalid number

How can I do that?

  • I tried to put an if statement before the input but that didn't work it out, then I tried to make something like a list compreension with an if statement and that gives me invalid syntax – Augusto D'Arruda Nov 04 '18 at 00:16
  • @AugustoD'Arruda: Did you try something like petruz's answer below? – Sheldore Nov 04 '18 at 00:19
  • The user needs to input a number so that you can check if that number falls between 1 and 50. Without inputting a number, you can not know whether it is a valid number or not. – Banghua Zhao Nov 04 '18 at 00:20
  • @Bazingaa I'm trying it right now. I was trying to do something and I couldn't work it out. – Augusto D'Arruda Nov 04 '18 at 00:23

4 Answers4

1
num = input("Choose a number between 1 and 50: ")

try:
    num = int(num) #Check if user input can be translated to integer. Otherwise this line will raise ValueError
    if not 1 <= num <= 50:
        print('Choose a number between 1 and 50: invalid number')
except ValueError:
    print('Choose a number between 1 and 50: invalid number')
1

You need the input to be set to an integer since otherwise it would be a string:

num = int(input("Choose a number between 1 and 50: "))

Check what num has been set to:

if 1 < num < 50:
    print(1)
else:
    print("invalid number")
petruz
  • 545
  • 3
  • 13
1

This is what I might do:

validResponse = False
while(not validResponse):
    try:
        num = int(input("Choose a number between 1 and 50: "))
        if(1 <= num <= 50):
            validResponse = True
        else:
            print("Invalid number.")
    except ValueError:
        print("Invalid number.")

This is, if you want to prompt them until a correct number has been entered. Otherwise, you can ditch the while loop and validResponse variable.

The try will run the statement until it runs into an error. if the error is specifically that the number couldn't be interpereted as an integer, then it raises a ValueError exception and the except statement tells the program what to do in that case. Any other form of error, in this case, still ends the program, as you'd want, since the only type of acceptable error here is the ValueError. However, you can have multiple except statements after the try statement to handle different errors.

Ethan Thomas
  • 71
  • 1
  • 5
0

In addition to the above answers, you can also use an assertion. You're likely going to use these more so when you're debugging and testing. If it fails, it's going to throw an AssertionError.

num = input('Choose a number between 1 and 50') # try entering something that isn't an int

assert type(num) == int
assert num >= 1
assert num <= 50

print(num)

You can use an if statement, but you need to assign the input to a variable first then include that variable in the conditional. Otherwise, you don't have anything to evaluate.

num = input('Choose a number between 1 and 50')

if type(num) == int: # if the input isn't an int, it won't print
    if num >= 1 and num <= 50:
        print(num) 

By default, the input provided is going to be a string. You can call the built-in function int() on the input and cast it to type int. It will throw a ValueError if the user enters something that isn't type int.

num = int(input('Choose a number between 1 and 50'))

You could also implement error handling (as seen in Moonsik Park's and Ethan Thomas' answers).