类型错误:索引列表必须是整数,而不是 str [英] TypeError: list of indices must be integers, not str

查看:76
本文介绍了类型错误:索引列表必须是整数,而不是 str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有什么问题给我带来了错误:类型错误:索引列表必须是整数,而不是 str

What is wrong in my code to give me the error: TypeError: List of indices must be integers, not 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()

推荐答案

你的最后一个循环是错误的.试试这个:

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

(顺便说一下,您的 3 个列表应该合并到一个字典列表中:

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

)

这篇关于类型错误:索引列表必须是整数,而不是 str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆