TypeError: list of indices must be integers, not str

a 夏天 提交于 2019-12-13 08:30:44

问题


What is wrong in my code to give me the error: TypeError: List of indices must be integers, not str

Here is my code:

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:


Your last loop is wrong. Try this:

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

(Your 3 lists should be merged in one list of dictionary by the way:

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()

)



来源:https://stackoverflow.com/questions/18075476/typeerror-list-of-indices-must-be-integers-not-str

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!