0

我想创建一个程序,从列表中选择一个团队并显示球员姓名和位置。

像这样的东西:

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")
4

3 回答 3

2

您可以通过将他们放入 dict 中来使您的团队更容易访问:

teams = { "A": ["John", "Peter", "Philip", "Ben"],
          "B": ["Bill", "Tommy", "Pete", "Manny"] }

然后:

print "At position", position, "is", teams[team][position - 1]

应该打印有问题的名称。您也将不得不更改团队打印display_team()

于 2013-08-26T09:45:18.653 回答
1

您可以Team先将列表放入字典中:

d = {'A':TeamA, 'B':TeamB}

然后,在您输入之后,您可以执行以下操作:

print "At position {0} is {1]".format(position, d[team][position - 1])

请记住,索引从 开始0,因此John从索引 0 开始。

于 2013-08-26T09:44:42.860 回答
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]
于 2013-08-26T10:11:15.680 回答