在我的程序中,我有一个包含用户输入创建的五个“玩家”的列表为了确定哪个玩家在两个会话中获得了最高分数,每个玩家都被问到相同的问题列表来确定他们的总分,然后显示。 然后再次询问球员相同的问题以确定他们第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