Askar Mujhib
Askar Mujhib

Reputation: 15

Comparison Operator & isnumeric() not compatible?

I want to check if whether input is numeric, If it is, Compare the number to another.

I tried to resolve one issue by removing int() conversion. But that would render x > 0 useless.

Tried searching online, But there's no simple resolve.

print("This program prints the sum of a range of numbers from x to y")
print("For example, if x is 10 and y is 50, the program will print the sum of numbers")
print("from 10 to 50")

x = int(input("Please enter the value of x: "))
y = int(input("Please enter the value of y: "))

if x.isnumeric() and y.isnumeric():
    if x > 0 and y > 0:
        print("Passed")
    else:
        print("failed")
else:
    print("One or more of your inputs are not numeric!")

Got 'int' object has no attribute 'isnumeric' as error or '>' not supported between instances of 'str' and 'int'

Upvotes: 1

Views: 245

Answers (1)

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

Your code has a few issues.

  1. str.isnumeric applies to strings, but you are trying to call it on an integer, hence you get the error 'int' object has no attribute 'isnumeric' as error

  2. If you then don't convert x to int, and leave it as str and try to do x > 0 and y > 0, you are comparing string and integer, which is not possible, as well, hence the error '>' not supported between instances of 'str' and 'int'

To resolve this, you can read x and y as a string, and then convert them to int when comparing them to 0 as follows.

print("This program prints the sum of a range of numbers from x to y")
print("For example, if x is 10 and y is 50, the program will print the sum of numbers")
print("from 10 to 50")

#Read x and y as strings
x = input("Please enter the value of x: ")
y = input("Please enter the value of y: ")

#Check if they are numeric
if x.isnumeric() and y.isnumeric():
    #Convert them to int to compare them with 0
    if int(x) > 0 and int(y) > 0:
        print("Passed")
    else:
        print("failed")
else:
    print("One or more of your inputs are not numeric!")

Then your output will look like

This program prints the sum of a range of numbers from x to y
For example, if x is 10 and y is 50, the program will print the sum of numbers
from 10 to 50
Please enter the value of x: 20
Please enter the value of y: 40
Passed

This program prints the sum of a range of numbers from x to y
For example, if x is 10 and y is 50, the program will print the sum of numbers
from 10 to 50
Please enter the value of x: 10
Please enter the value of y: 60
Passed

But hold on, what happened here

This program prints the sum of a range of numbers from x to y
For example, if x is 10 and y is 50, the program will print the sum of numbers
from 10 to 50
Please enter the value of x: -20
Please enter the value of y: -40
One or more of your inputs are not numeric!

Here I will make a note here that this logic won't work for negative integers as you can see below, isnumeric returns False for negative integers

In [1]: '-20'.isnumeric()                                                                                                                             
Out[1]: False

Hence a much better approach might be is to try to cast the string as an int, and based on that, check if x is a number or not

print("This program prints the sum of a range of numbers from x to y")
print("For example, if x is 10 and y is 50, the program will print the sum of numbers")
print("from 10 to 50")

#Read x and y as strings
x = input("Please enter the value of x: ")
y = input("Please enter the value of y: ")

def is_number(s):

    #If we can cast string to int, it is an int, else it is not
    is_number = False
    try:
        int(s)
        is_number = True
    except ValueError:
        pass

    return is_number

#Check if they are numeric
if is_number(x) and is_number(y):
    #Convert them to int to compare them with 0
    if int(x) > 0 and int(y) > 0:
        print("Passed")
    else:
        print("failed")
else:
    print("One or more of your inputs are not numeric!")

Now the code will work for negative integers too

This program prints the sum of a range of numbers from x to y
For example, if x is 10 and y is 50, the program will print the sum of numbers
from 10 to 50
Please enter the value of x: -20
Please enter the value of y: -40
failed

This program prints the sum of a range of numbers from x to y
For example, if x is 10 and y is 50, the program will print the sum of numbers
from 10 to 50
Please enter the value of x: hey
Please enter the value of y: hi
One or more of your inputs are not numeric!

Upvotes: 2

Related Questions