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")
当我输入小于 80 且大于 80 的数字时,它会打印被拒绝的消息。有人看到我错过了什么吗?
height or width or length > 80
被评估为(height) or (width) or (length > 80)
。在这种情况下,如果任何浮点数都不同于 0,则任何浮点数都将被视为真实,并且一旦 Python 可以确定结果,评估就会停止。
因此,在您的情况下,如果height
不为零,则表达式将被视为True
。
您应该使用:
if height > 80 or width > 80 or length > 80:
或:
if any(dimension > 80 for dimension in (height, width, length)):
它应该是:
if height > 80 or width > 80 or length > 80:
print("Rejected, measurements exceed 80cm.")
语句if height
(不带比较部分(将返回true
如果height != 0