如果不满足条件,则跳过迭代

时间:2017-04-04 14:46:45

标签: python list loops conditional-statements

目前正在搞乱幻想足球节目,要求用户首先输入10个添加到列表中的玩家名称,然后为每个玩家输入不同变量的值,如得分,目标助攻等。第一个问题是"他们参加了比赛吗?"

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

t1 = t1.replace("'","")

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

2 个答案:

答案 0 :(得分:0)

据我所知,你只需要继续正常,自然地结束if语句

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

答案 1 :(得分:0)

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

因为你有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 --->"))

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

enter image description here

编辑:好的,这里是你的代码修好后,在你输入他们的信息或说“不”后检查每个新玩家。

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