gpt4 book ai didi

Python 日历类型错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:44:28 24 4
gpt4 key购买 nike

我正在尝试创建一个日历并为其中一个工作日安排一个时间段。

timetable = [[""] * 24 for day in range(7)]

timetable[0][15] = "meeting with Jane"

# first let's define weekday names
WEEKDAYS = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday')

# now we iterate over each day in the timetable

for day in timetable:

# and over each timeslot in each day

day_name = WEEKDAYS[day]

for i, event in enumerate(day):
if event: # if the slot is not an empty string
print("%s at %02d:00 -- %s" % (day_name, i, event))

我得到错误:

day_name = WEEKDAYS[day]
TypeError: tuple indices must be integers, not list

我知道“day”作为列表传递给导致此错误的元组,但我应该对我的代码进行哪些修改才能解决此问题?

最佳答案

使用更多的枚举!

for index, day in enumerate(timetable):
day_name = WEEKDAYS[index]
for i, event in enumerate(day):
if event: # if the slot is not an empty string
print("%s at %02d:00 -- %s" % (day_name, i, event))

关于Python 日历类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41962955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com