2017-04-25 58 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:只要檢查你的問題是否已在此問題上得到解決。 –