如何将if语句输出结果转换为python脚本

如何将if语句输出结果转换为python脚本,python,if-statement,input,sensors,Python,If Statement,Input,Sensors,我正在尝试实现一个light sensors输入,以便在较大的python脚本中输出一个值 具体打印LED_亮度=75或LED_亮度=255 基本上,当这个脚本运行时,我希望传感器为LED的字符串指定LED亮度 我开始试着做以下事情 import urllib2 import xml.etree.ElementTree as ET import time from neopixel import * import sys import os import RPi.GPIO as GPIO

我正在尝试实现一个light sensors输入,以便在较大的python脚本中输出一个值

具体打印LED_亮度=75或LED_亮度=255

基本上,当这个脚本运行时,我希望传感器为LED的字符串指定LED亮度

我开始试着做以下事情

import urllib2
import xml.etree.ElementTree as ET
import time
from neopixel import *
import sys
import os    
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)

if GPIO.input(4) == 1:
        print "LED_BRIGHTNESS = 75"
else:
        print "LED_BRIGHTNESS = 255"
# LED strip configuration:
LED_COUNT      = 95      # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN        = 10      # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
#LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53
LED_STRIP      = ws.WS2811_STRIP_GRB   # Strip type and colour ordering
import urllib2
import xml.etree.ElementTree as ET
import time
from neopixel import *
import sys
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)


if GPIO.input(4) == 1: "LED_BRIGHTNESS = 75"
if GPIO.input(4) == 0: "LED_BRIGHTNESS = 255"


# LED strip configuration:
LED_COUNT      = 95      # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN        = 10      # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
#LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53
LED_STRIP      = ws.WS2811_STRIP_GRB   # Strip type and colour ordering
这将把结果输出到终端,但我需要它输出到更大的脚本中

于是我厌倦了下面的

import urllib2
import xml.etree.ElementTree as ET
import time
from neopixel import *
import sys
import os    
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)

if GPIO.input(4) == 1:
        print "LED_BRIGHTNESS = 75"
else:
        print "LED_BRIGHTNESS = 255"
# LED strip configuration:
LED_COUNT      = 95      # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN        = 10      # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
#LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53
LED_STRIP      = ws.WS2811_STRIP_GRB   # Strip type and colour ordering
import urllib2
import xml.etree.ElementTree as ET
import time
from neopixel import *
import sys
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)


if GPIO.input(4) == 1: "LED_BRIGHTNESS = 75"
if GPIO.input(4) == 0: "LED_BRIGHTNESS = 255"


# LED strip configuration:
LED_COUNT      = 95      # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN        = 10      # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
#LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53
LED_STRIP      = ws.WS2811_STRIP_GRB   # Strip type and colour ordering
这将返回以下错误

Traceback (most recent call last):
  File "metar.py", line 30, in <module>
    strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
NameError: name 'LED_BRIGHTNESS' is not defined
回溯(最近一次呼叫最后一次):
文件“metar.py”,第30行,在
条带=新像素(LED条带计数、LED条带引脚、LED条带频率、LED条带DMA、LED条带反转、LED条带亮度、LED条带通道、LED条带)
名称错误:未定义名称“LED_亮度”
我对这一点太陌生了,我甚至不知道用谷歌搜索这个问题时应该使用什么正确的术语。我尝试了如何将if语句输出输出到字符串中,或者如何将if语句输出到字符串中,并且我不断地发现一些关于完全预定义值的内容,而不是来自sensorif语句的东西


如果有人能给我指出正确的方向,我将不胜感激。

看起来你想动态设置一个变量。在这种情况下,由于只有两个可能的值,内联if将执行以下操作:

LED_BRIGHTNESS = 75 if GPIO.input(4) == 1 else 255
或者,您可以在以下情况下扩展:

if GPIO.input(4) == 1:
    LED_BRIGHTNESS = 75
else:
    LED_BRIGHTNESS = 255

你肯定不是想把你的代码包装成一个字符串…?我想是的。我需要它输出,以便LED_亮度值基于传感器输入。您只想动态设置变量吗?如果GPIO.input(4)=1或者255,那么它的发光二极管亮度=75。谢谢,我想这很简单,只是这里没有线索。非常感谢你的时间。