循环/嵌套循环问题Python 2.7 [英] Loop/Nested Loop problems Python 2.7

查看:70
本文介绍了循环/嵌套循环问题Python 2.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望程序向五个玩家中的每一个问七个问题,计算玩家得分,然后显示每个玩家得分的列表,例如 第一周的积分 玩家1 43 玩家2 26 player3 38等

I want the program to ask each of the five players the seven questions, calculate the player score and then display a list of each players score eg Points for week 1 player1 43 player2 26 player3 38 etc etc

然后再次问问玩家问题,并在第二周做同样的事情.

Then ask the players the questions again and do the same for week 2.

当前程序仅显示第一个玩家得分,然后再次向所有五个玩家询问问题,但仅显示第二个玩家得分,它会重复此操作五次,而仅遍历所有玩家.

Currently the program will only display the first players score, then ask the questions to all five players again but only display the second players score, it repeats this five times while only iterating through the players.

我要去哪里了,任何帮助将不胜感激

Where am I going wrong, any help will be much appreciated

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

推荐答案

好吧,我想如果您通过使用另一个函数来稍微改变信息的调用方式,并且在这种情况下使用字典而不是列表来获取信息,玩家/得分信息,您将能够更好地管理代码更改,并且更轻松地操作数据.

Ok so I think if you change how your call the information a little by using another function and you use a dictionary instead of a list in this case for getting the information of player/score you will be better able to manage changes to the code and also have an easier time manipulating the data.

她是我到目前为止所得到的.

Her is what I got so far.

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位玩家的结果.

This should result in something returned like the following. Note: I changed the amount of players being looked at to 2 to make testing go faster. so this information below will only show the results of looking at 2 players.

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 

这篇关于循环/嵌套循环问题Python 2.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆