【问题标题】:Skipping iteration if condition not met如果条件不满足则跳过迭代
【发布时间】:2017-08-29 20:22:42
【问题描述】:

目前正在搞乱一个梦幻足球程序,要求用户首先输入 10 名球员姓名并将其添加到一个列表中,然后为每个球员输入不同变量的值,如进球、进球助攻等。第一个问题是“他们参加比赛了吗?”

如果答案是否定的,那么我将尝试跳过以下问题并跳转到下一个玩家,我一直在尝试使用 continue,但它只是循环并继续向第一个玩家询问第一个问题。

    playerList=[]

def Playeradd():
    playerList.append(item)


def Playercreate():
    global item
    item = raw_input("Enter Player name: ")

    Playeradd()



[Playercreate()for _ in range (5)]

print
print "You have selected", len(playerList), "players for your squad, Your selected squad is.."
#print playerList
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
        print "Please enter score for ", playerList[x]

        print

        play = raw_input(" Did he play the match (yes or no?) ")
        if play == "yes":
                play1=2
        else:
                 play1=0


        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



        playerpoint1= play1+goalS+goalA+yelC1+redC1+PenM1

        PlayerandScore= [playerList[x],playerpoint1,]
        scorecheck.append(PlayerandScore)
        totalscore+= playerpoint1

   # print "This player has scored a total of ", PlayerandScore, " this week "
        x+= 1
        y+= 1
        print "This player has scored a total of ", PlayerandScore, " this week "
        print

pointsaward()

抱歉,如果不清楚,那么对于玩家 1 如果第一个问题的答案是否定的,那么就没有必要问关于玩家的以下问题,因为他不会玩。所以我希望它跳过以下问题并开始询问列表中玩家 2 的输入。

【问题讨论】:

  • 也许你想 break 退出循环?
  • 请发布完整代码。很难理解“但它只是循环并继续向第一个玩家询问第一个问题”。代码应该做什么?
  • 我会将你所有的 IF 语句移到第一个 IF 下。这样,如果是,则执行所有其他 IF。如果不是,则返回问题。

标签: python list loops conditional-statements


【解决方案1】:

据我所知,您只需要照常继续,自然结束 if 语句

play = input(" Did he play the match (yes or no?) ")
if play == "yes":
    play1=2
else:
      play1=0
print("Continue Here")

【讨论】:

  • 我需要它来跳过以下问题并开始询问我列表中的下一个玩家
【解决方案2】:

我认为您应该将问题放在函数中。这将允许您随时调用该函数。 并更容易决定何时提出问题。

因为你有 raw_input 我假设你使用的是 python 2.X

您可以将 python2 或 python3 添加到您的标签中,以便从更了解每个版本的 python 的人那里获得更好的帮助。

list_of_players = ["player 1","player 2","player 3"]

def check_player(yes_no):
    if yes_no == "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
        return "no"
        print "player did not play"
# this for statement will ask about each player in the list of players.  
for player in list_of_players: 
    print "Did "+player+" play in the game?"
    print check_player(raw_input("yes or no --->"))

如果您运行我的代码,这是控制台的图像。

编辑:好的,您的代码已修复,以便在您输入他们的信息或说“不”后检查每个新玩家。

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

【讨论】:

  • 抱歉,我不得不稍微清理一下代码。我在问题中更新了它。试试新代码。
  • 注意:您应该添加您的列表。这将帮助我为你拼凑一些东西。我没有意识到您正在为这个问题添加一个列表。
  • 假设您想传入一个列表,我添加了我的代码。如果这是您想要做的,那么我的答案中的代码现在会要求您对每个玩家说是或否
  • @Grimble6 让我知道这个更新的代码是否有帮助。它似乎对我有用。
  • 我不断收到错误,我的列表是通过用户输入创建的,如果有帮助,我用完整代码更新了原始问题
猜你喜欢
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
  • 2020-01-01
  • 2017-10-05
  • 2014-12-19
相关资源
最近更新 更多