繁体   English   中英

VS Code 中的 Python 告诉我一个数字小于一个较小的数字

[英]Python in VS Code is telling me a number is smaller than a smaller number

这段代码是用 VS Code 编写的,Python。 我的代码中有一个最小变量和另一个变量。 我们称它们为 X 和 Xmin。 我给 Xmin 和 X 数字。 然后,当我将它们与 < 进行比较时,我的代码告诉我较小的较大。 这是我的代码

Xmin = 100
print("X")
X = input()
if X < Xmin:
    print("X is too small.")

问题是当我让 X = 500 时,它会告诉我 X 大于 Xmin,但是当我给 X 一个非常大的东西时,比如 1000000,它会告诉我 X 太小了。

如果您使用的是 python 3,则需要在输入语句周围添加一个 int(),以便 python 知道用户输入应该是数字,而不是字符串:

try:

    Xmin = 100
    print("X")
    X = int(input())
    if X < Xmin:
        print("X is too small.")

except:
    print('That is not an integer.')

如果您使用的是 python 2,请注意,python 2 中的 input() 相当于 python 3 中的 eval(input())。我们都知道“eval 是邪恶的”。

X = input() #takes input as string

使用下面的代码而不是上面的代码:

X = int(input()) #takes input as integer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM