首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于Python 2中变量内容的LED照明和熄灭故障

基于Python 2中变量内容的LED照明和熄灭故障
EN

Stack Overflow用户
提问于 2016-01-15 22:11:12
回答 3查看 56关注 0票数 2

我在一个气象站工作,它从加拿大环境部的数据中提取多伦多的数据。如果风速和风寒的结果超过或低于一定的数字,我想在面包板上点燃LED。

风速警告使用以下代码正确工作:

代码语言:javascript
复制
if ((cityObject.get_quantity(wind)) > windmax: GPIO.output(13,GPIO.HIGH)
else: GPIO.output(13,GPIO.LOW)

风寒警告被证明更为复杂。加拿大环境部在没有明显的寒风的情况下返回“无”。但是,最终,它也会返回一个负整数。下面的代码点亮我的风寒警告LED时,真的,它应该关闭:

代码语言:javascript
复制
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)

在编写本报告时,以下内容不返回

代码语言:javascript
复制
print cityObject.get_quantity(chill)

知道为什么这段代码会让我的LED保持亮着吗?我只希望当风寒低于-15C时才能启动。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-01-16 15:19:09

在这种情况下,问题似乎是Python没有将加拿大环境部的风寒结果作为一个整数来处理,尽管结果包含数字。这段代码修复了它。

代码语言:javascript
复制
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)
票数 0
EN

Stack Overflow用户

发布于 2016-01-15 22:14:57

我假设您混淆了字符串"None"和类型None

print cityObject.get_quantity(chill)将打印None的类型表示,因此您将看到没有打印。

而不是:

代码语言:javascript
复制
if (cityObject.get_quantity(chill)) == chillzero:

试着:

代码语言:javascript
复制
if cityObject.get_quantity(chill) is not None:

此外,似乎也有一些红色的调用,所以您也可以尝试这样做:

代码语言:javascript
复制
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)
票数 0
EN

Stack Overflow用户

发布于 2016-01-16 23:47:16

由于这是您的第一个任何类型的编码项目,所以您可能只是在某个地方遇到了数据类型问题。当有疑问的时候,只要一步一步地去做,如果需要的话。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34820745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档