Python 对函数内的变量使用多个值

Python 对函数内的变量使用多个值,python,Python,我有以下功能(来自blinkstick库): 它基本上打开通过USB连接到我的电脑的led条中的第一个led(index=0)index=1将打开第二个,依此类推 我希望index参数存储从0到31的多个值(因为条带中有32个LED) 我知道一个简单而懒惰的解决方法是只编写32次函数,然后手动更改索引,但是有没有更聪明的方法来存储0-31的值呢 我试过: while x < 32: bstick.set_color(channel=0, index=x, name="wh

我有以下功能(来自blinkstick库):

它基本上打开通过USB连接到我的电脑的led条中的第一个led(
index=0
index=1
将打开第二个,依此类推

我希望
index
参数存储从0到31的多个值(因为条带中有32个LED)

我知道一个简单而懒惰的解决方法是只编写32次函数,然后手动更改
索引
,但是有没有更聪明的方法来存储0-31的值呢

我试过:

while x < 32:
    bstick.set_color(channel=0, index=x, name="white")
    x+=1
当x<32时:
b点击设置颜色(通道=0,索引=x,名称=“白色”)
x+=1
但这并不是我真正想要的,因为到目前为止,这与我的项目其余部分的编写方式不太友好,如下所示:

from datetime import datetime, time
import pandas
import ctypes
from playsound import playsound
from blinkstick import blinkstick

bstick = blinkstick.find_first()

def bstick_turn_on():
    x=0
    while x < 32:
        bstick.set_color(channel=0, index=x, name="white")
        x+=1

def bstick_turn_off():
    x=0
    while x < 32:
        bstick.set_color(channel=0, index=x)
        x+=1

file_path = "myfile.xlsx" #sunrise/sunset file path
data = pandas.read_excel(file_path, header=0) #Header on line 0

#Today as day number in reference to 1st of Jan
day = datetime.now().timetuple().tm_yday

#Today's parameters
#sr and ss are column names in the Excel spreadsheet for sunrise and sunset respectively
#Minus 1 to account for 0 based indexing
sunrise = data["sr"][day - 1]
sunset = data["ss"][day - 1] 

#Time right now
now = datetime.now().time()

#Function to convert time objects into integers
def seconds_in_time(time_value: time):
    return (time_value.hour * 60 + time_value.minute) * 60 + time_value.second

#Variable for a moment in time 5 minutes before the sunset
sunset_minus_five = seconds_in_time(sunset) - 60 * 5

#Setting up the day_night variable depending on the now variable
#delta calculates the difference in seconds between now and sunset -during night- and sunrise -during day-
#A negative value for delta means that now variable is equal to any moment between midnight and the sunrise  
if now > sunrise and now < sunset:
    day_night = 'day'
    delta = (seconds_in_time(now) - seconds_in_time(sunrise))
else:
    day_night = 'night'
    delta = (seconds_in_time(now) - seconds_in_time(sunset))

#delta_notification calculates the difference in seconds between now and sunset_minus_five
delta_notification = seconds_in_time(now) - sunset_minus_five
    
#The path to the wallpapers being used
path = 'C:\\Users\\mariu\\Desktop\\Sunset\\wallpapers\\'+ day_night +'.jpg'

#Function defined to perform an action (open/close the light) depending on the time of the day
def on_off():
    if now > sunrise and now < sunset:
        return bstick_turn_on()
    else: 
        return bstick_turn_off()

#Function to change the wallpaper
def changeBG(path):
    ctypes.windll.user32.SystemParametersInfoW(20, 0, path, 3) #SystemParametersInfoW for x64 architecture

#An alarm sound is played and a red light turns on if delta_notification is less or equal than 15 seconds AND delta_notification is greater than -30 delta_notification <= 15 and delta_notification => -30:
if delta_notification <= 15 and delta_notification >= -30:
    playsound('alarm.wav') #Plays the sound
    bstick_turn_on()

#Wallpaper changes, a three-beep sound is played, and light turns on only if delta is less than 60 seconds AND delta is greater than -1
#In order for the light to turn on, the script should be ran on a computer that is on the same network as the light bulb (wireless)
if delta <= 15 and delta >= -30:
    changeBG(path)
    playsound('sound.mp3') #Plays the sound
    on_off()
从日期时间导入日期时间,时间
进口大熊猫
导入ctypes
从playsound导入playsound
从blinkstick导入blinkstick
bstick=blinkstick.find_first()
def B单击打开()
x=0
当x<32时:
b点击设置颜色(通道=0,索引=x,名称=“白色”)
x+=1
def bstick_turn_off():
x=0
当x<32时:
b点击设置颜色(通道=0,索引=x)
x+=1
file_path=“myfile.xlsx”#日出/日落文件路径
data=pandas.read_excel(文件路径,页眉=0)#第0行的页眉
#今天作为1月1日的天数
day=datetime.now().timetuple().tm_yday
#今天的参数
#sr和ss分别是Excel电子表格中日出和日落的列名
#减去1以说明基于0的索引
日出=数据[“sr”][第1天]
日落=数据[“ss”][第1天]
#现在正是时候
now=datetime.now().time()
#函数将时间对象转换为整数
def秒数单位时间(时间值:时间):
返回(时间值.hour*60+时间值.minute)*60+时间值.second
#日落前5分钟的一瞬间变化
日落时间减去五秒钟(日落时间)-60*5
#根据now变量设置昼夜变量
#delta计算了现在与日落(夜间)和日出(白天)之间的秒差-
#delta的负值表示now变量等于午夜到日出之间的任何时刻
如果现在>日出,现在<日落:
昼夜=‘白天’
delta=(秒时间(现在)-秒时间(日出))
其他:
昼夜=‘夜’
delta=(秒(现在)-秒(日落))
#delta_通知以秒为单位计算现在和日落_减去五之间的差值
delta_通知=秒(现在)-日落时间减五秒
#正在使用的墙纸的路径
路径='C:\\Users\\mariu\\Desktop\\Sunset\\wallpapers\\\'+day\u night+'.jpg'
#定义用于根据一天中的时间执行操作(打开/关闭灯)的函数
def on_off():
如果现在>日出,现在<日落:
返回b单击打开()
其他:
返回b单击关闭()
#功能更改壁纸
def changeBG(路径):
ctypes.windell.user32.SystemParametersInfoW(20,0,path,3)#适用于x64体系结构的SystemParametersInfoW
#如果delta_通知小于或等于15秒且delta_通知大于-30 delta_通知-30:
如果delta_通知=-30:
播放声音(“alarm.wav”)#播放声音
b单击打开()
#墙纸改变,发出三声蜂鸣声,只有当增量小于60秒且增量大于-1时,灯才会亮起
#为使灯亮起,应在与灯泡位于同一网络上的计算机上运行脚本(无线)
如果增量=-30:
更改bg(路径)
播放声音(“sound.mp3”)#播放声音
开闭

由于所有32盏灯似乎都同时打开或关闭,我建议使用参数创建一个函数

def set_light(color=None, count=32):
    args = {'channel': 0}
    if color is not None:
        args['name'] = color
    for x in range(count):       
        bstick.set_color(**args, index=x)

若要关闭,请执行以下操作:
set_light()
,若要打开,请执行以下操作:
set_light('white')

这里的解决方案是循环所有数字,这就是您所做的,我认为没有问题it@GamopoOP应该在这里使用for循环,但我同意
def set_light(color=None, count=32):
    args = {'channel': 0}
    if color is not None:
        args['name'] = color
    for x in range(count):       
        bstick.set_color(**args, index=x)