从循环Python 2.7中的列表存储变量

时间:2017-04-25 17:57:27

标签: python-2.7 list loops variables global

在我的计划中,我有一个包含五个“玩家”的列表。通过用户输入创建,为了确定哪个玩家在两个会话中获得最高分,五个玩家中的每一个被问到相同的问题列表以确定他们的总分然后被显示。 然后再次向玩家询问相同的问题以确定他们在第2周的得分

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

每个玩家应该有两个单独的分数,即第1周和第2周

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()

1 个答案:

答案 0 :(得分:0)

我添加了一个新变量用于存储每周的总数:

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 ...