【问题标题】:Loop/Nested Loop problems Python 2.7循环/嵌套循环问题 Python 2.7
【发布时间】:2017-09-20 20:33:35
【问题描述】:

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

然后再次向玩家提问,并在第 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()

【问题讨论】:

    标签: python-2.7 loops variables while-loop nested


    【解决方案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 
    

    【讨论】:

    • 效果更好,我还是 pyhon 新手,所以我需要学习字典。我试图让它在第二周再次提问,但它一直在打印一个空列表
    • 我需要看看你在第二周是如何提出这个问题的 你可能想问另一个问题 :D 。你可以用字典、列表和元组做很多事情。它只是弄清楚什么最适合这种情况。您甚至可以格式化打印信息的方式,以便丢弃所有不需要的字符,例如{} : ' "
    • 我只是想再次问同样的问题,然后打印出球员名单以及第一周和第二周的综合得分
    • 最好的选择是创建一个可以存储组合分数的全局变量。
    • 另外,您要问的是与此问题“代码明智”不同的问题。听起来您希望能够为每个单独的玩家组合每周的得分。在您尝试自己创建代码之后,最好在单独的问题中提出这个问题。我无法在这里回答这样的问题,因为它会使阅读原始帖子的人感到困惑。所以最好的选择是提出一个新问题,以便正确回答
    猜你喜欢
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    相关资源
    最近更新 更多