Zach
Zach

Reputation: 11

How do I omit strings and other characters within conditionals in Python 3?

I'm trying to create a small code that asks a user to enter a number between 1 and 100. However my Else statement will not output when entering a string. I want my Else statement to print a message if the user enters an input other than an integer or float. Here is my code.

def between():
    print ("Please enter a number between 1 and 100.")
    number = eval(input())
    if number >= 1 and number <= 100:
        print ("Thank you! You entered",number,"which is within the 1 and 100 range.")
        between()
    elif number > 100 or number < 1: 
        print ("OOPS! You entered",number,"Please enter a number between 1 and 100.")
        between()
    else: 
        print ("ERROR! You have entered an invalid value. Please try again using numerical values only.")
        between()

between()

Upvotes: 0

Views: 208

Answers (5)

Shailyn Ortiz
Shailyn Ortiz

Reputation: 766

Try is too slow and should be avoided when not necessary

num = input("Please enter a number between 1 and 100: ")

if num.replace('.','').isdigit():
    num = float(num)
    if number >= 1 and number <= 100:
        print ("Thank you! You entered",number,"which is within the 1 and 100 range.")
        between()
    else:
        print ("OOPS! You entered",number,"Please enter a number between 1 and 100.")
        between()
else:
    print('thats not a number!')

Upvotes: 0

penguin2048
penguin2048

Reputation: 1343

There are a few logical issues with your code. First before using eval() you must be aware of the security risk associated with it. Consider going through this post for some insight. If you entered some arbitrary input in eval, it will most likely be a run time error.

Now, assuming that the variable actually has a value. To check if it is an integer or a float and is in the desired range or something else (possibly a string), you can not directly apply comparison operators with it because if the variable is not a number it will most likely give a TypeError during comparison with other integers.

Hence a correct approach would be to first check if the variable is of desired type by checking if type(r) is int or type(r) is float only then you should apply test for range. If the above type checking is false then you should print your "else" error message.

Upvotes: 0

wedg
wedg

Reputation: 11

The easiest method would be to use try and except

num = input()
try:
    number = float(num)
    # code to do if num is a number
except ValueError:
    print("Not a number!")
    # code to do if num isn't a number

If num isn't a number, then converting it to a float would raise ValueError going on to the except. If num is a number, the coversion of it to a float would run fine, and continue on in the try.

Side note: I highly suggest not using eval(input()) but rather just input(), see this for why you shouldn't use eval. Also eval(input()) won't work with my example above.

Upvotes: 1

Gustavo Topete
Gustavo Topete

Reputation: 1306

Try:

def between():
    print ("Please enter a number between 1 and 100.")
    number=0
    try:
        number = int(input())
    except:
        print ("ERROR! You have entered an invalid value. Please try again using numerical values only.")
        exit(1)
    if number >= 1 and number <= 100:
        print ("Thank you! You entered",number,"which is within the 1 and 100 range.")
        between()
    else: 
        print ("OOPS! You entered",number,"Please enter a number between 1 and 100.")
        between()


 between()

Note: why you use eval and input together? I don't think that's right, correct me if I'm wrong

Upvotes: 0

Adi219
Adi219

Reputation: 4814

Try this:

print "Please enter a number between 1 and 100"
in = input()


try:
    num = int(in)

    if 1 <= num <= 100:
        print("Valid")
    else:
        print("Out of bounds")

except:

    print("Invalid")

Upvotes: 0

Related Questions