Я пытаюсь вызвать функции в python 2.7, представив список и попросив пользователя выбрать индекс элемента в списке. Я смущен в двух пунктах:Вызывающие функции с пользователем Введенные индексы
Как пользовательский индекс может соответствовать функции, определенной в исходном списке? Я понимаю, как выбирать элементы в списке, но не понимаю, как вызвать функцию.
Где я должен помещать этот блок кода? В основной функции?
Вот мой код: Спасибо
''' 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