比较运算符和isnumeric()不兼容?

时间:2019-05-04 05:25:25

标签: python-3.x

我要检查输入是否为数字,如果是,则将数字与另一个进行比较。

我试图通过删除int()转换来解决一个问题。但这会使x> 0失效。

尝试在线搜索,但是没有简单的解决方法。

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'对象没有属性'isnumeric'作为错误 要么 'str'和'int'的实例之间不支持'>'

2 个答案:

答案 0 :(得分:2)

您的代码有几个问题。

  1. str.isnumeric适用于字符串,但是您试图在整数上调用它,因此会收到错误信息''int'对象没有属性'isnumeric'作为错误信息

  2. 如果您随后不将x转换为int,而将其保留为str并尝试执行x > 0 and y > 0,则您正在比较字符串和整数,这不是也可能,因此错误'>' not supported between instances of 'str' and 'int'

要解决此问题,可以将xy读为字符串,然后在将它们与0比较时将它们转换为int,如下所示。

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!")

然后您的输出将如下所示

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

但是等等,这里发生了什么

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!

在这里,我将在此处进行说明,该逻辑不适用于负整数 如下所示,isnumeric对负整数返回False

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

因此,更好的方法可能是尝试将字符串强制转换为int,然后基于此检查x是否为数字

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:
        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!")

现在代码也将适用于负整数

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!

答案 1 :(得分:0)

您必须检查输入是否为数字,然后将其转换为int

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

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