Python:为什么if语句不能检测变量是大于还是小于80? print(“=~=~=~=~=~=~=~=~=~=~==”) 打印(“”) 高度=浮动(输入(“输入高度(厘米):”) 宽度=浮动(输入(“输入宽度(厘米):”) 长度=浮动(输入(“输入长度(cm):”) 打印(“”) 打印(“=~=~=~=~=~=~=~=~=~=~=~==”) 如果高度、宽度或长度>80: 打印(“拒绝,测量值超过80厘米”) elif高度或宽度或长度80被评估为(高度)或(宽度)或(长度>80)。在此上下文中,如果任何浮点值与0不同,那么它将被视为真实值,并且一旦Python确定结果,计算将立即停止
当我输入小于80和大于80的数字时,它会打印被拒绝的消息。有人看到我遗漏了什么吗?应该是:Python:为什么if语句不能检测变量是大于还是小于80? print(“=~=~=~=~=~=~=~=~=~=~==”) 打印(“”) 高度=浮动(输入(“输入高度(厘米):”) 宽度=浮动(输入(“输入宽度(厘米):”) 长度=浮动(输入(“输入长度(cm):”) 打印(“”) 打印(“=~=~=~=~=~=~=~=~=~=~=~==”) 如果高度、宽度或长度>80: 打印(“拒绝,测量值超过80厘米”) elif高度或宽度或长度80被评估为(高度)或(宽度)或(长度>80)。在此上下文中,如果任何浮点值与0不同,那么它将被视为真实值,并且一旦Python确定结果,计算将立即停止,python,if-statement,logic,Python,If Statement,Logic,当我输入小于80和大于80的数字时,它会打印被拒绝的消息。有人看到我遗漏了什么吗?应该是: print("=~=~=~=~=~=~=~=~=~=~=") print("") height = float(input("Input the height (cm): ")) width = float(input("Input the width (cm): ")) length = float(input("Input the length (cm): ")) print("") print("=
print("=~=~=~=~=~=~=~=~=~=~=")
print("")
height = float(input("Input the height (cm): "))
width = float(input("Input the width (cm): "))
length = float(input("Input the length (cm): "))
print("")
print("=~=~=~=~=~=~=~=~=~=~=")
if height or width or length > 80:
print("Rejected, measurements exceed 80cm.")
elif height or width or length < 80:
print("Works")
else:
print("Error")
语句
if height
(不带比较部分)将返回true
ifheight!=0
高度或宽度或长度>80
被评估为(高度)或(宽度)或(长度>80)
。在此上下文中,如果任何浮点值与0不同,那么它将被视为真实值,并且一旦Python确定结果,计算将立即停止
因此,在您的情况下,如果height
不是零,则表达式将被视为True
你应使用:
if height > 80 or width > 80 or length > 80:
print("Rejected, measurements exceed 80cm.")
或:
如果高度、宽度或长度>80:
无法按预期工作。这被评估为:如果(高度)或(宽度)或(长度>80):
。
if height > 80 or width > 80 or length > 80:
if any(dimension > 80 for dimension in (height, width, length)):