我正在尝试创建一个Python调度程序,我试图在某一天安排会议。
以下是代码:
timetable = [[""] * 24 for slots in range(7)]
WEEKDAYS = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday')
timetable[0][15] = "meeting with Jane"
for day in timetable:
for i, event in enumerate(day):
if event:
print("%s at %02d:00 -- %s" % (WEEKDAYS[day], i, event))
然而,当我运行上面的代码时,我收到一个错误;
Traceback (most recent call last):
File "C:\Users\workspace\test\2D.py", line 18, in <module>
print("%s at %02d:00 -- %s" % (WEEKDAYS[day], i, event))
TypeError: tuple indices must be integers, not list
我正打算安排会议安排的日子,例如:周一与简会面或周六与简会面。
我如何打印确切的一天?
答案 0 :(得分:0)
好的,问题是在WEEKDAYS[day]
中,day
是24个事件的列表;您想再次使用enumerate()
:
timetable = [[""] * 24 for slots in range(7)]
WEEKDAYS = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday')
timetable[0][15] = "meeting with Jane"
for j, day in enumerate(timetable):
# day is a list of 24 entries
for i, event in enumerate(day):
if event:
print("%s at %02d:00 -- %s" % (WEEKDAYS[j], i, event))