【发布时间】:2020-11-19 19:51:35
【问题描述】:
当插入输入 b1 为 300,b2 为 1000 为什么这段代码的输出显示“b1>b2”
虽然 b2 是任何小于 1000 的数字,但会给出正确的输出“b2>b1”
不使用输入函数给b1&b2赋值不会导致这个问题
b1 = input("Enter balance 1: ")
b2 = input("Enter balance 2: ")
list = [739,324,45]
def calculate_balance():
if b1>b2:
bl = int(b1)
bs = int(b2)
print("b1>b2")
elif b1==b2:
bl = int(b1)
bs = int(b2)
print("b1=b2")
else:
bl = int(b2)
bs = int(b1)
print("b2>b1")
calculate_balance()
【问题讨论】:
-
在进行比较之前,您需要转换为整数。
-
您正在比较值之前将它们转换为
ints。只需执行b1 = int(input("Enter balance 1:"))等,以便它们从一开始就是int。 -
input()返回字符串值,因此b1和b2是字符串,因此您的比较使用 alphabetic 排序而不是 numeric 订购。
标签: python for-loop comparison-operators