我在一个气象站工作,它从加拿大环境部的数据中提取多伦多的数据。如果风速和风寒的结果超过或低于一定的数字,我想在面包板上点燃LED。
风速警告使用以下代码正确工作:
if ((cityObject.get_quantity(wind)) > windmax: GPIO.output(13,GPIO.HIGH)
else: GPIO.output(13,GPIO.LOW)风寒警告被证明更为复杂。加拿大环境部在没有明显的寒风的情况下返回“无”。但是,最终,它也会返回一个负整数。下面的代码点亮我的风寒警告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)在编写本报告时,以下内容不返回
print cityObject.get_quantity(chill)知道为什么这段代码会让我的LED保持亮着吗?我只希望当风寒低于-15C时才能启动。
发布于 2016-01-16 15:19:09
在这种情况下,问题似乎是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)发布于 2016-01-15 22:14:57
我假设您混淆了字符串"None"和类型None。
print cityObject.get_quantity(chill)将打印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)发布于 2016-01-16 23:47:16
由于这是您的第一个任何类型的编码项目,所以您可能只是在某个地方遇到了数据类型问题。当有疑问的时候,只要一步一步地去做,如果需要的话。
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)https://stackoverflow.com/questions/34820745
复制相似问题