2017-04-24 37 views
1

我希望該計劃要求每五個玩家的七個問題,計算選手的分數,然後顯示的每個球員名單得分如 點爲第1周 PLAYER1 43 player2 26 player3 38等等等等環路/嵌套循環問題的Python 2.7

然後再問玩家的問題,併爲一週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=[] 
x=0 
totalscore=0 
def pointsaward(): 
    global scorecheck, totalscore 
    y=1 
    player=y 
    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?) ") 
       if motm == "yes": 
        motm1=5 
       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 
      else: 
       play1=0 
       print player+" did not play" 
     playerpoint1= play1+goalS+goalA+yelC1+redC1+PenM1 
     PlayerandScore= [playerList[x],playerpoint1,] 
     scorecheck.append(PlayerandScore) 
     totalscore+= playerpoint1 
     x+= 1 
     y+= 1 
     print "This player has scored a total of ", PlayerandScore, " this week " 
pointsaward() 

回答

1

好了,所以我想,如果你一點通過其他方法改變如何您的通話信息和您使用字典而不是列表在這種情況下,爲了獲得玩家/分數的信息,您將能夠更好地管理對代碼的更改,並且還可以更輕鬆地操作數據。

她是我到目前爲止。

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

這應該會導致返回如下內容。注意:我將玩家數量改爲2,以使測試更快。所以下面的這些信息只會顯示看2名球員的結果。

Enter Player name: ads 
Enter Player name: qwe 
You have selected 2 players for your squad, Your selected squad is.. 
ads 
qwe 
Please enter score for ads 
Did ads play in the game? 
Did he play the match (yes or no?) yes 
Did he score, if so how many?5 
How many assists?5 
Did he win man of the match (yes or no?) yes 
Did he recieve a yellow card (yes or no?) no 
Did he recieve a red card (yes or no?) no 
Did he miss a peno(yes or no?) no 
Did qwe play in the game? 
Did he play the match (yes or no?) no 
This player has scored a total of {'ads': 47, 'qwe': 'qwe did not play'} this week 
+0

這工作更好,我仍然是新的pyhon,所以我需要了解字典。我試圖讓它再次提出問題的第二週,但它不斷打印一個空列表 – Grimble6

+0

我需要看看你是如何問這個問題的第二週你可能想問另一個問題:D。你可以用字典,列表和元組做很多事情。它只是一個確定什麼將最適合於這種情況的問題。你甚至可以設置打印信息的方式,這樣就可以丟棄所有不需要的字符,比如'{}:'「' –

+0

我只是想再次提問相同的問題,然後用第一週的綜合分數打印出玩家列表和兩個 – Grimble6