我想创建一个程序,从列表中选择一个团队并显示玩家名称和位置。
这样的事情:
Enter the team: A
Enter the position number: 1
然后它应该打印出这样的东西:
At postion 1 is John
这是我到目前为止所得到的:
def display_team(TeamNum, Team):
print "Team" + TeamNum + ": "
for player in Team:
print player
#main
#Lists used to define the teams
TeamA = ["John", "Peter", "Philip", "Ben"]
TeamB = ["Bill", "Tommy", "Pete", "Manny"]
display_team('A', 'TeamA')
display_team('B', 'TeamB')
team = raw_input("Enter the team: ")
position = int(raw_input("Enter the position:"))
raw_input("\nPress enter to continue")
答案 0 :(得分:2)
您可以通过将其置于词典中来更轻松地访问您的团队:
teams = { "A": ["John", "Peter", "Philip", "Ben"],
"B": ["Bill", "Tommy", "Pete", "Manny"] }
然后:
print "At position", position, "is", teams[team][position - 1]
应打印相关名称。您还必须在display_team()
更改团队打印。
答案 1 :(得分:1)
您可以先将Team
列表放入字典中:
d = {'A':TeamA, 'B':TeamB}
然后,在您输入后,您可以:
print "At position {0} is {1]".format(position, d[team][position - 1])
请记住,索引从0
开始,因此John
位于索引0处。
答案 2 :(得分:0)
将其添加到代码的末尾:
if 'A' is team:
print 'TEAM A\n', 'At Position ', position , TeamA[position - 1]
if 'B' is team:
print 'TEAM B\n', 'At Position ', position , TeamB[position - 1]