我正在为第一个选项制作一个有趣的快捷菜单,如下面的代码所示。它给了我一些索引错误
TypeError: list indices must be integers, not str
但是,我为使用类似格式的购物清单制作的一些代码完全正常。有什么想法吗?
#Prototype Menu II
import sys
print ("""
Hello and WELCOME to the...
__ ___
/ |/ /__ ____ __ __
/ /|_/ / _ \/ __ \/ / / /
/ / / / __/ / / / /_/ /
/_/ /_/\___/_/ /_/\__,_/
Please enter one of the three following options!
1 - Entering Player Names
2 - Opening up a game
3 - Exit the Program
""")
choice = input("")
if choice == "1":
players = []
for count in range(2):
name = input("Player Name:")
players.append(name)
for each in range(len(players)):
print ("{0} ".format(players[name]))
答案 0 :(得分:2)
您必须使用each
作为列表索引而不是name
这是一个字符串
print ("{0} ".format(players[each]))
而不是循环range
players
长players
,您可以直接在for player in players:
print ("{0} ".format(player))
上循环
{{1}}