我一直在试图编写这个程序,并寻找修复/这个问题的答案。我是写Python的新手。Python少于/大于问题
BatPower = "100"
if BatPower >= "70":
print("BatPower - GOOD")
elif BatPower <= "50":
print("BatPower - OK")
elif BatPower <= "30":
print("BatPower - CRITICAL")
else:
print("CRITICAL ERROR READING BATPOWER!")
所以你可以看到 'BatPower = “100”' 但不是返回值 “BatPower - GOOD” 返回 “BatPower - 确定”。当我输入'BatPower =“2”'时。它返回“BatPower - 确定”
这里有一些其他的输入与输出有:
BatPower = "75"
BatPower - GOOD
BatPower = "30"
BatPower - OK
BatPower = "29"
BatPower - OK
BatPower = "99"
BatPower - GOOD
BatPower = "1"
BatPower - OK
正如你所看到的,我从来没有得到过临界产量。良好的输出效果很好,直到它到达100
编辑: 最后的工作代码:
BatPower = 60
BatStatus = "Passing..."
if BatPower >= 70:
BatStatus="BatPower - GOOD"
elif BatPower >= 30:
BatStatus="BatPower - OK"
elif BatPower >= 0:
BatStatus="BatPower - CRITICAL"
else:
BatStatus="CRITICAL ERROR READING BATPOWER!"
print(BatStatus)
感谢的帮助!
不要使用字符串来指定权力 - Python无法比较那样的字符串。使用数字代替 – darthbith
将字符串[按照字典顺序](http://en.wikipedia.org/wiki/Lexicographical_order)进行比较,例如''100'''2''。 – jonrsharpe
@MartinPieters:OP有另一个与字符串比较无关的问题。程序的逻辑看起来不会返回CRITICAL,因为任何数字'n <= 30'都满足'n <= 50',这首先被评估。我想在回答中解决这个问题。 – ericmjl