TypeError:索引列表必须是整数,而不是str

时间:2013-08-06 08:43:39

标签: python python-3.3

我的代码中有什么错误给我错误:     TypeError:索引列表必须是整数,而不是str

这是我的代码:

print("This programe will keep track of your TV schedule.")
Finish = False
Show = []
ShowStart = []
ShowEnd = []
while not Finish:
print()
ShowName = input("What is the shows name?: ")
if ShowName == "":
    Finish = True
else:
    ShowStartTime = input("What time does the show start?: ")
    ShowEndTime = input("What time does the show end?: ")
    Show.append(ShowName)
    ShowStart.append(ShowStartTime)
    ShowEnd.append(ShowEndTime)
print("{0:<10}  |  {1:<10}  |  {2:<10}  ".format("Show Name", "Start Time", "End Time"))
for each in Show:
print("{0:<10}  |  {1:<10}  |  {2:<10}  ".format(Show[each], ShowStart[each],  ShowEnd[each]))
input()

1 个答案:

答案 0 :(得分:0)

你的上一次循环是错误的。试试这个:

for each in range(len(Show)):
    print("{0:<10}  |  {1:<10}  |  {2:<10}  ".format(Show[each], ShowStart[each],  ShowEnd[each]))

(顺便说一下,你的3个名单应该合并在一个字典列表中:

print("This programe will keep track of your TV schedule.")
Finish = False
shows = []
while not Finish:
    ShowName = input("What is the shows name?: ")
    if ShowName == "":
        Finish = True
    else:
        ShowStartTime = input("What time does the show start?: ")
        ShowEndTime = input("What time does the show end?: ")
        shows.append({'name': ShowName, 'start': ShowStartTime, 'end': ShowEndTime})

print("{0:<10}  |  {1:<10}  |  {2:<10}  ".format("Show Name", "Start Time", "End Time"))

for item in shows:
    print("{0:<10}  |  {1:<10}  |  {2:<10}  ".format(item['name'], item['start'],  item['end']))
    # Or  the more pythonic way:
    print("{name:<10} | {start:<10} | {end:<10} ".format(**item)
input()