2017-04-25 57 views
0

在我的程序中,我有一个包含用户输入创建的五个“玩家”的列表为了确定哪个玩家在两个会话中获得了最高分数,每个玩家都被问到相同的问题列表来确定他们的总分,然后显示。 然后再次询问球员相同的问题以确定他们第2周的得分从循环列表中存储变量Python 2.7

如何将每位球员得分存储为全局变量?

每个球员应该有两个单独的分数,即

playerList=[] 
def Playeradd(): 
    playerList.append(item) 
def Playercreate(): 
    global item 
    item = raw_input("Enter Player name: ") 

    Playeradd() 

[Playercreate()for _ in range (5)] 
print "You have selected", len(playerList), "players for your squad, Your selected squad is.." 

for item in playerList: 
    print item 

player =Playercreate 
scorecheck={} # using a dictionary rather than a list. Because you only have to values to look at this to me seams the best option for displaying data. 
x=0 
totalscore=0 
def pointsaward(): 
    global x, totalscore,scorecheck 
    scorecheck={} 
    while x < 5: 
     print "Please enter score for ", playerList[x] 
     for player in playerList: 
      print "Did "+player+" play in the game?" 
      play = raw_raw_input(" Did he play the match (yes or no?) ") 
      if play == "yes": 
       play1=2 
       goalS= int(raw_input(" Did he score, if so how many?")) 
       goalS=goalS*5 
       goalA= int(raw_input(" How many assists?")) 
       goalA=goalA*3 
       motm= raw_input(" Did he win man of the match (yes or no?) ") 
       motm1=0 
       yelC1=0 
       redC1=0 
       PenM1=0 
       if motm == "yes": 
        motm1=5 #this was missing from the math in total points 
       else: 
        motm1=0 
       yelC=raw_input(" Did he recieve a yellow card (yes or no?) ") 
       if yelC == "yes": 
        yelC1= -1 
       else: 
        yelC1=0 
       redC=raw_input(" Did he recieve a red card (yes or no?) ") 
       if redC == "yes": 
        redC1= -5 
       else: 
        redC1=0        
       PenM=raw_input(" Did he miss a peno(yes or no?) ") 
       if PenM == "yes": 
        PenM1= -3 
       else: 
        PenM1=0 
       playerpoint1= play1+goalS+goalA+yelC1+redC1+PenM1+motm1 
       scorecheck[playerList[x]] = playerpoint1 
       x+= 1 
      else: 
       play1=0 
       scorecheck[playerList[x]] = (player+" did not play") 
       x+= 1 

def printResults(): # added a simple function run the point adding function and print the results. 
    pointsaward() 
    print "This player has scored a total of ", scorecheck, " this week " 
printResults() 
+0

你是否已经开始为此可以提供任何代码?您不需要发布所有*代码,只需要一些关注问题的简短片段,并帮助我们更好地了解您的问题。 – davedwards

+0

@downshift我现在的代码已经对不起 – Grimble6

+0

我看到它谢谢你:)。我一直在评论它,看起来你已经有了'scorecheck = {}''全球''。我认为这已经成为每个球员得分的字典,不是吗?既然你可以用'scorecheck ['player_name']'来获得'player_name'的分数。 – davedwards

回答

0

我添加了一个新的变量Week1和Week2被用来存储总量每周:

weekly_scores= {}

我有添加一个单独的功能,以便将周后的所有分数相加:

查看代码块中的注释以查看解释。

def combineScores(): 
    global scorecheck, weekly_scores 
    print "These player have scored a total of ", scorecheck, " this week " 

#this for loop is used to define the scores for any case of players playing or not.  
    for player in scorecheck: 
     # if the player has never played a game this season prior to this week. 
     if player not in weekly_scores: 
      # if player did not play this season or this game   
      if scorecheck[player] == (player+" did not play"): 
       weekly_scores[player] = 0 
      # player did not play prior to this week but did play this week. 
      else: 
       newScore = scorecheck[player] 
       weekly_scores[player] = newScore 
       print player+" combined score : "+str(newScore) 
     # Player has played prior to this weeks game 
     else: 
      # played prior to this week but did not play this week. 
      if scorecheck[player] == (player+" did not play"): 
       oldScore = weekly_scores[player] 
       newScore = 0 
       combined = oldScore+newScore 
       weekly_scores[player] = combined 
       print player+" combined score : "+str(combined) 
      #played prior to this week and played this week also 
      else: 
       oldScore = weekly_scores[player] 
       newScore = scorecheck[player] 
       combined = oldScore+newScore 
       weekly_scores[player] = combined 
       print player+" combined score : "+str(combined) 

# Having this allows me to keep track of the scores for the entire season 
# and takes into account when the player is not in every game. 

在我刚打电话printResults()遍地脱颖而出每星期我想补充的总结果代码的结束。这只是例如原因。我会定义一个函数,如果我是你决定要记录多少个星期,然后多次调用printResults()

以下是我为您提供的完整工作代码:如果您有任何问题,请告知我。

playerList=[] 
def Playeradd(): 
    playerList.append(item) 
def Playercreate(): 
    global item 
    item = raw_input("Enter Player name: ") 

    Playeradd() 

[Playercreate()for _ in range (5)] 
print "You have selected", len(playerList), "players for your squad, Your selected squad is.." 

for item in playerList: 
    print item 

player =Playercreate 
scorecheck={} # using a dictionary rather than a list. Because you only have to values to look at this to me seams the best option for displaying data. 

totalscore=0 

weekly_scores= {} 

def pointsaward(): 
    global totalscore,scorecheck 
    x=0 
    while x < 5: 
     print ("Please enter score for ", playerList[x]) 
     for player in playerList: 
      print "Did "+player+" play in the game?" 
      play = raw_input(" Did he play the match (yes or no?) ") 
      if play == "yes": 
       play1=2 
       goalS= int(raw_input(" Did he score, if so how many?")) 
       goalS=goalS*5 
       goalA= int(raw_input(" How many assists?")) 
       goalA=goalA*3 
       motm= raw_input(" Did he win man of the match (yes or no?) ") 
       motm1=0 
       yelC1=0 
       redC1=0 
       PenM1=0 
       if motm == "yes": 
        motm1=5 #this was missing from the math in total points 
       else: 
        motm1=0 
       yelC=raw_input(" Did he recieve a yellow card (yes or no?) ") 
       if yelC == "yes": 
        yelC1= -1 
       else: 
        yelC1=0 
       redC=raw_input(" Did he recieve a red card (yes or no?) ") 
       if redC == "yes": 
        redC1= -5 
       else: 
        redC1=0        
       PenM=raw_input(" Did he miss a peno(yes or no?) ") 
       if PenM == "yes": 
        PenM1= -3 
       else: 
        PenM1=0 
       playerpoint1= play1+goalS+goalA+yelC1+redC1+PenM1+motm1 
       scorecheck[playerList[x]] = playerpoint1 
       x+= 1 
      else: 
       play1=0 
       scorecheck[playerList[x]] = (player+" did not play") 
       x+= 1 
    combineScores() 

def printResults(): # added a simple function run the point adding function and print the results. 
    pointsaward() 
    #print "These player has scored a total of ", scorecheck, " this week " 


def combineScores(): 
    global scorecheck, weekly_scores 

    print ("These player has scored a total of ", scorecheck, " this week ") 
    print() 

    for player in scorecheck: 
     if player not in weekly_scores:    
      if scorecheck[player] == (player+" did not play"): 
       weekly_scores[player] = 0 
      else: 
       newScore = scorecheck[player] 
       weekly_scores[player] = newScore 
       print player+" combined score : "+str(newScore) 

     else: 
      if scorecheck[player] == (player+" did not play"): 
       oldScore = weekly_scores[player] 
       newScore = 0 
       combined = oldScore+newScore 
       weekly_scores[player] = combined 
       print player+" combined score : "+str(combined) 
      else: 
       oldScore = weekly_scores[player] 
       newScore = scorecheck[player] 
       combined = oldScore+newScore 
       weekly_scores[player] = combined 
       print player+" combined score : "+str(combined) 
printResults() 
#second_week = 
printResults() 
#third_week = 
printResults() 
#fourth_week = 
printResults() 
#and so on ... 
+0

当我试图运行代码我收到错误 ''回溯(最近通话最后一个): 文件 “C:/Python27/HUH.py”,第93行,在 printResults() 文件“C:/Python27/HUH.py “,第71行,在printResults pointsaward() 文件”C:/ Python27/HUH。py“,第68行,点数 combineScores() 文件”C:/Python27/HUH.py“,第88行,在combineScores oldScore = weekly_scores [player] KeyError:'James'' – Grimble6

+0

@ Grimble6好吧我想编辑:必须将代码添加到这两个部分我的不好 –

+0

@ Gimpble6:只要检查你的问题是否已在此问题上得到解决。 –