gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:58:55 25 4
gpt4 key购买 nike

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

我尝试通过删除 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com