[英]Simple Python IF statement does not seem to be working
I'm new in python and I'm writing a simple script to work with Firebase but I'm stuck on a simple if statement that seems not working as expected:我是 python 新手,我正在编写一个简单的脚本来使用 Firebase,但我被困在一个简单的 if 语句上,它似乎没有按预期工作:
## check for max values
if humidity > maxHumidity:
firebase.put("/Controls/Sensors", "/Humidity/max_inside", ""+humidity+"%")
print("Updating Humidity max_inside")
if temperature > maxTemperature:
firebase.put("/Controls/Sensors", "/Temperature/max_inside", ""+temperature+"C")
print("Updating Temperature max_inside")
## check for min values
if humidity < minHumidity:
firebase.put("/Controls/Sensors", "/Humidity/min_inside", ""+humidity+"%")
print("Updating Humidity min_inside")
if temperature < minTemperature:
firebase.put("/Controls/Sensors", "/Temperature/min_inside", ""+temperature+"C")
print("Updating Temperature min_inside")
The problem is that the first two if statements are working as expected, the last two not.. If the humidity < minHumidity, nothing happens.问题是前两个 if 语句按预期工作,后两个不......如果湿度 < minHumidity,则什么也不会发生。
The value that I'm using are double like 70.50..我使用的值是 70.50 的两倍。
Edit编辑
##retrieve max & min humidity (remove the %)
maxHumidity = firebase.get("/Controls/Sensors/Humidity/max_inside", None)
maxHumidity = maxHumidity[:-1]
maxHumidity = float(maxHumidity)
minHumidity = firebase.get("/Controls/Sensors/Humidity/min_inside", None)
minHumidity = minHumidity[:-1]
minHumidity = float(minHumidity)
#retrieve max & min temperature (remove the C)
maxTemperature = firebase.get("/Controls/Sensors/Temperature/max_inside", None)
maxTemperature = maxTemperature[:-1]
maxTemperature = float(maxTemperature)
minTemperature = firebase.get("/Controls/Sensors/Temperature/min_inside", None)
minTemperature = minTemperature[:-1]
minTemperature = float(minTemperature)
#add current value
humidity, temperature = readDHT22()
firebase.put("/Controls/Sensors", "/Humidity/current_inside", ""+humidity+"%")
firebase.put("/Controls/Sensors", "/Temperature/current_inside", ""+temperature+"C")
##check for max values
if humidity > maxHumidity:
firebase.put("/Controls/Sensors", "/Humidity/max_inside", ""+humidity+"%")
print("Updating Humidity max_inside")
if temperature > maxTemperature:
firebase.put("/Controls/Sensors", "/Temperature/max_inside", ""+temperature+"C")
print("Updating Temperature max_inside")
## cehck for min values
if humidity < minHumidity:
firebase.put("/Controls/Sensors", "/Humidity/min_inside", ""+humidity+"%")
print("Updating Humidity min_inside")
if temperature < minTemperature:
firebase.put("/Controls/Sensors", "/Temperature/min_inside", ""+temperature+"C")
print("Updating Temperature min_inside")
Tried to use float but seems still not working!尝试使用浮动,但似乎仍然无法正常工作! Is not going into the min ifs.
不会进入 min ifs。
You are comparing strings with floating point numbers.您正在将字符串与浮点数进行比较。
minTemperature
, maxTemperature
, minHumidity
and maxHumidity
are all float
objects because you converted them. minTemperature
、 maxTemperature
、 minHumidity
和maxHumidity
都是float
对象,因为您对它们进行了转换。 But temperature
and humidity
are strings , because otherwise Python would have thrown an exception when you tried concatenating them with other strings.但是
temperature
和humidity
是字符串,否则当您尝试将它们与其他字符串连接时,Python 会抛出异常。
Compare float
to float
by converting either in the test:通过在测试中进行转换来比较
float
和float
:
if float(humidity) > maxHumidity:
etc. or by converting humidity
and temperature
to floats and converting them back to strings when inserting into Firebase.等等,或者通过将
humidity
和temperature
转换为浮点数,然后在插入 Firebase 时将它们转换回字符串。
In Python 2, different types of objects are always consistently sorted, with numbers coming before other types.在 Python 2 中,不同类型的对象总是一致地排序,数字排在其他类型之前。 This means that
<
and >
comparisons are true or false based on the sort order of the two operands, and since numbers in Python 2 are sorted first, any comparison with another type is done with the number considered smaller :这意味着
<
和>
比较是基于两个操作数的排序顺序是真还是假,并且由于 Python 2 中的数字首先排序,与另一种类型的任何比较都使用较小的数字完成:
>>> 3.14 < "1.1"
True
Python 3 does away with trying to make everything orderable; Python 3 不再试图让一切都变得可排序; comparing a float to a string gives you a
TypeError
exception instead.将浮点数与字符串进行比较会给您一个
TypeError
异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.