根据 Python 2 中变量的内容,问题点亮和熄灭 LED
Trouble lighting and extinguishing an LED based on the contents of a variable in Python 2
我正在研究一个从加拿大环境部多伦多数据中提取数据的气象站。如果风速和风寒结果超过或低于特定数值,我想点亮面包板上的 LED。
使用以下代码风速警告正常工作:
if ((cityObject.get_quantity(wind)) > windmax: GPIO.output(13,GPIO.HIGH)
else: GPIO.output(13,GPIO.LOW)
事实证明,风寒警告更加复杂。当没有明显的风寒时,加拿大环境部 return 是 "None"。但是,最终,它也会 return 一个负整数。以下代码点亮了我的风寒警告 LED,实际上它应该关闭:
chillzero = "None"
if (cityObject.get_quantity(chill)) == chillzero: GPIO.output(11,GPIO.LOW)
if (cityObject.get_quantity(chill)) < chillmax: GPIO.output(11,GPIO.HIGH)
else: GPIO.output(11,GPIO.LOW)
在撰写本文时,以下 return 秒 None
print cityObject.get_quantity(chill)
知道为什么这段代码会导致我的 LED 一直亮着吗?我只希望它在风寒低于 -15C
.
时开启
我假设您混淆了字符串 "None"
和类型 None
。
print cityObject.get_quantity(chill)
将打印 None
的类型表示,这就是为什么您会看到 None 被打印出来的原因。
而不是:
if (cityObject.get_quantity(chill)) == chillzero:
试试看:
if cityObject.get_quantity(chill) is not None:
也有一些冗余调用,所以你也可以试试这个:
if (cityObject.get_quantity(chill)) < chillmax:
print "Windchill: %s" % cityObject.get_quantity(chill)
GPIO.output(11, GPIO.HIGH)
else:
# Would be greater than or equal to chillmax, or return None
GPIO.output(11, GPIO.LOW)
在这种情况下,问题似乎是 Python 没有将加拿大环境部的风寒结果视为整数,尽管结果包含数字。此代码修复了它。
chillmax = int(cityObject.get_quantity(chill))
if (cityObject.get_quantity(chill)) is not None:
if chillmax < -15:
GPIO.output(11,GPIO.HIGH)
else:
GPIO.output(11,GPIO.LOW)
由于这是您的第一个编码项目,因此您可能只是在某个地方遇到了数据类型问题。当有疑问时,如果需要的话,就一步一步地去做。
try:
chill_value=cityObject.get_quantity(chill)
print "Windchill is: "+str(chill_value)
except:
print "Unable to get chill value for some reason."
exit(1)
try:
if (float(chill_value) < float(chillmax)):
print "Turning on LED"
GPIO.output(11, GPIO.HIGH)
else:
print "Turning off LED"
GPIO.output(11, GPIO.LOW)
except:
#It should not make it here unless one of your values is not a
#float, or you have some other issue with being able to compare
#the values.
print "Unable to compare chill value. Defaulting to off."
GPIO.output(11, GPIO.LOW)
我正在研究一个从加拿大环境部多伦多数据中提取数据的气象站。如果风速和风寒结果超过或低于特定数值,我想点亮面包板上的 LED。
使用以下代码风速警告正常工作:
if ((cityObject.get_quantity(wind)) > windmax: GPIO.output(13,GPIO.HIGH)
else: GPIO.output(13,GPIO.LOW)
事实证明,风寒警告更加复杂。当没有明显的风寒时,加拿大环境部 return 是 "None"。但是,最终,它也会 return 一个负整数。以下代码点亮了我的风寒警告 LED,实际上它应该关闭:
chillzero = "None"
if (cityObject.get_quantity(chill)) == chillzero: GPIO.output(11,GPIO.LOW)
if (cityObject.get_quantity(chill)) < chillmax: GPIO.output(11,GPIO.HIGH)
else: GPIO.output(11,GPIO.LOW)
在撰写本文时,以下 return 秒 None
print cityObject.get_quantity(chill)
知道为什么这段代码会导致我的 LED 一直亮着吗?我只希望它在风寒低于 -15C
.
我假设您混淆了字符串 "None"
和类型 None
。
print cityObject.get_quantity(chill)
将打印 None
的类型表示,这就是为什么您会看到 None 被打印出来的原因。
而不是:
if (cityObject.get_quantity(chill)) == chillzero:
试试看:
if cityObject.get_quantity(chill) is not None:
也有一些冗余调用,所以你也可以试试这个:
if (cityObject.get_quantity(chill)) < chillmax:
print "Windchill: %s" % cityObject.get_quantity(chill)
GPIO.output(11, GPIO.HIGH)
else:
# Would be greater than or equal to chillmax, or return None
GPIO.output(11, GPIO.LOW)
在这种情况下,问题似乎是 Python 没有将加拿大环境部的风寒结果视为整数,尽管结果包含数字。此代码修复了它。
chillmax = int(cityObject.get_quantity(chill))
if (cityObject.get_quantity(chill)) is not None:
if chillmax < -15:
GPIO.output(11,GPIO.HIGH)
else:
GPIO.output(11,GPIO.LOW)
由于这是您的第一个编码项目,因此您可能只是在某个地方遇到了数据类型问题。当有疑问时,如果需要的话,就一步一步地去做。
try:
chill_value=cityObject.get_quantity(chill)
print "Windchill is: "+str(chill_value)
except:
print "Unable to get chill value for some reason."
exit(1)
try:
if (float(chill_value) < float(chillmax)):
print "Turning on LED"
GPIO.output(11, GPIO.HIGH)
else:
print "Turning off LED"
GPIO.output(11, GPIO.LOW)
except:
#It should not make it here unless one of your values is not a
#float, or you have some other issue with being able to compare
#the values.
print "Unable to compare chill value. Defaulting to off."
GPIO.output(11, GPIO.LOW)