python - 比较运算符与 isnumeric() 不兼容?

标签 python 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!")

“int”对象没有“isnumeric”属性作为错误 或者 'str' 和 'int' 实例之间不支持 '>'

最佳答案

您的代码有一些问题。

  1. str.isnumeric适用于字符串,但您尝试在整数上调用它,因此您会收到错误 'int' object has no attribute '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 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!")

现在代码也适用于负整数

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!

关于python - 比较运算符与 isnumeric() 不兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55979735/

相关文章:

python - 用于将包发布到 PyPi 的实用程序?

python - 对象可以检查分配给它的变量的名称吗?

python - 尽管安装了torch vision pytorch 库,但我收到一条错误消息,指出没有名为torch vision 的模块

python - 计算第一次出现和个体出现之间的差异(Python Pandas)

python-3.x - 调试 Numpy VisibleDeprecationWarning(来自不规则嵌套序列的 ndarray)

python - 发电机自身的产物

python - 等效于 Python pandas 中的 'mutate_at' dplyr 函数

python - 快速检查列表是否是嵌套列表元素之一的子列表

python-3.x - 使用 LSTM 训练的模型仅预测所有值相同的值

python-3.x - tf.global_variables_initializer() 不起作用