简单的Python IF语句似乎不起作用 [英] Simple Python IF statement does not seem to be working

查看:213
本文介绍了简单的Python IF语句似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我正在编写一个简单的脚本来与Firebase一起使用,但是我陷入了一个简单的if语句,该语句似乎无法按预期工作:

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:

## 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")

问题在于前两个if语句按预期工作,后两个if语句不工作. minHumidity,什么也没发生.

The problem is that the first two if statements are working as expected, the last two not.. If the humidity < minHumidity, nothing happens.

我使用的值是70.50的两倍.

The value that I'm using are double like 70.50..

修改

##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")

试图使用浮点数,但似乎仍然无法正常工作!不会进入最小的ifs.

Tried to use float but seems still not working! Is not going into the min ifs.

推荐答案

您正在将字符串与浮点数进行比较.

You are comparing strings with floating point numbers.

minTemperaturemaxTemperatureminHumiditymaxHumidity都是float对象,因为您已对其进行了转换.但是temperaturehumidity字符串,因为否则,当您尝试将它们与其他字符串连接时,Python会抛出异常.

minTemperature, maxTemperature, minHumidity and maxHumidity are all float objects because you converted them. But temperature and humidity are strings, because otherwise Python would have thrown an exception when you tried concatenating them with other strings.

通过在测试中进行转换,将floatfloat进行比较:

Compare float to float by converting either in the test:

if float(humidity) > maxHumidity:

等或将humiditytemperature转换为浮点数,然后在插入Firebase时将其转换回字符串.

etc. or by converting humidity and temperature to floats and converting them back to strings when inserting into Firebase.

在Python 2中,始终对不同类型的对象进行始终排序,数字排在其他类型之前.这意味着根据两个操作数的排序顺序,<>比较是对还是错,并且由于Python 2中的数字首先被排序,因此与其他类型的任何比较都将被认为是 smaller的数字进行:

In Python 2, different types of objects are always consistently sorted, with numbers coming before other types. 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:

>>> 3.14 < "1.1"
True

Python 3消除了使所有东西都可排序的企图.比较浮点数和字符串会给您一个TypeError异常.

Python 3 does away with trying to make everything orderable; comparing a float to a string gives you a TypeError exception instead.

这篇关于简单的Python IF语句似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆