简体   繁体   English

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

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

This code was written in VS Code, Python.这段代码是用 VS Code 编写的,Python。 I have a minimum variable in my code and another variable.我的代码中有一个最小变量和另一个变量。 Let's call them X and Xmin.我们称它们为 X 和 Xmin。 I give Xmin and X numbers.我给 Xmin 和 X 数字。 Then when I compare them with < my code tells me that the smaller one is larger.然后,当我将它们与 < 进行比较时,我的代码告诉我较小的较大。 Here is my code这是我的代码

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

The problem is when I make X = 500, it will tell me that X is greater than Xmin, but when I give X something really big, like 1000000, it will tell me that X is too small.问题是当我让 X = 500 时,它会告诉我 X 大于 Xmin,但是当我给 X 一个非常大的东西时,比如 1000000,它会告诉我 X 太小了。

If you are using python 3, you need to add an int() around the input statement in order for python to know the user input should be a number, not a string:如果您使用的是 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.')

If you are using python 2, watch out, input() in python 2 is the equivalent of eval(input()) in python 3. and we all know that 'eval is evil'.如果您使用的是 python 2,请注意,python 2 中的 input() 相当于 python 3 中的 eval(input())。我们都知道“eval 是邪恶的”。

X = input() #takes input as string

Use below code instead of above:使用下面的代码而不是上面的代码:

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

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

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