私は、リストを提示し、ユーザにリスト内のアイテムのインデックスを選択するように求めて、Python 2.7で関数を呼び出そうとしています。私は2つの点で混乱しています:ユーザが入力したインデックスを使って関数を呼び出す
ユーザが入力したインデックスは、初期リストに定義されている関数とどのように対応していますか?私はリスト内の項目を選択する方法を理解していますが、関数を呼び出す方法を理解していません。
このコードブロックはどこに配置すればよいですか?主な機能は?ここで
は私のコードです:
''' neoPixel setups'''
import time
from neopixel import *
# LED configuration.
LED_COUNT = 16 # Number of LED pixels.
LED_PIN = 12 # GPIO pin connected to the pixels (must support PWM!).
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)
def colorWipe(strip, color, wait_ms=50):
"""Wipe color across display a pixel at a time."""
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
strip.show()
time.sleep(wait_ms/1000.0)
def theaterChaseRainbow(strip, wait_ms=50):
"""Rainbow movie theater light style chaser animation."""
for j in range(256):
for q in range(3):
for i in range(0, strip.numPixels(), 3):
strip.setPixelColor(i + q, wheel((i + j) % 255))
strip.show()
time.sleep(wait_ms/1000.0)
for i in range(0, strip.numPixels(), 3):
strip.setPixelColor(i + q, 0)
if __name__ == '_main_':
# create neopixel object with appropriate configuration
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
# initialize the library (must be called before other function)
strip.begin()
print 'Press Ctrl-C to quit'
''' user input block '''
lighting_modes = 'rainbow', 'colorWipe'
for i, item in enumerate(lighting_modes):
print i, item
user_input = input("Please enter index of the lighting modes listed above")
if user_input == 0:
colorWipe(strip, Color(255, 0, 0)) # red
colorWipe(strip, Color(0, 255, 0)) # blue
colorWipe(strip, Color(0, 0, 255)) # green
elif user_input == 1:
theaterChase(strip, Color(127, 127, 127))
theaterChase(strip, Color(127, 0, 0))
theaterChase(strip, Color(0, 0, 127))
'' '文字列を返しますinput'''、' ''してみてくださいもしUSER_INPUT == '0''''と '' '場合USER_INPUT ==' 1'''' – wwii
右のおかげで、私はそれをキャストすることもできますが、これは関数呼び出しにどのように対応しますか? –
私はあなたが何を求めているのか確認しています。リストから関数をインデックスで選択して呼び出すのですか? – wwii