在我的程序中,我有一個包含用戶輸入創建的五個「玩家」的列表爲了確定哪個玩家在兩個會話中獲得了最高分數,每個玩家都被問到相同的問題列表來確定他們的總分,然後顯示。 然後再次詢問球員相同的問題以確定他們第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()
你是否已經開始爲此可以提供任何代碼?您不需要發佈所有*代碼,只需要一些關注問題的簡短片段,並幫助我們更好地瞭解您的問題。 – davedwards
@downshift我現在的代碼已經對不起 – Grimble6
我看到它謝謝你:)。我一直在評論它,看起來你已經有了'scorecheck = {}''全球''。我認爲這已經成爲每個球員得分的字典,不是嗎?既然你可以用'scorecheck ['player_name']'來獲得'player_name'的分數。 – davedwards