Python 比较运算符在 if-else 语句中没有给出正确的答案

问题描述 投票:0回答:1
# Program1: Take two numbers as input from User and print which one is greater or are they equal.

num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
if num1 is num2:
    print("The two numbers are equal")
elif num1 < num2:
    print("The first number is less than the second number")
else:
    print("The first number is greater than the second number")


输入第一个数字:-10 输入第二个数字:-20 第一个数字小于第二个数字

进程已完成,退出代码为 0

上面比较两个数字的代码有什么问题? python 为什么缺少基本的数字比较?

python-3.x comparison-operators
1个回答
0
投票

在您的情况下,您应该使用“==”语句而不是“is”语句。

'=='实际上是比较'is'的值,检查它们是否是同一个对象。

请参阅之前有关该问题的讨论

© www.soinside.com 2019 - 2024. All rights reserved.