类型错误:索引列表必须是整数,而不是str的
问题描述:
有什么错我的代码给我的错误: 类型错误:指数的名单必须是整数,而不是str的类型错误:索引列表必须是整数,而不是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()
答
您上一次循环错误。试试这个:
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()
)
+0
如果你打算使用'dict',那么使用命名参数比如'print(“{name:
+0
当然,但我不想过于复杂化代码(我不认为作者知道'** var'语法:-))。我添加您的解决方案作为替代。 –
+0
谢谢你真的为我清除了这一点,我也学到了新的东西感谢。我是一个你肯定猜到的小白菜:D –
当你用Google搜索“类型错误:索引列表必须是整数,不是str“ - 对你没有帮助/你从结果中不了解什么? –