为什么我不能在if语句中打破?

时间:2016-04-03 19:47:11

标签: python-2.7

我有这段代码:

turn2l = []
turn1l = [] 
for i in range(5):

turn1 = raw_input("Enter co-ordinates player 1: ")
turn1l.append(turn1)
turn2 = raw_input("Enter co-ordinates player 2: ")
turn2l.append(turn2)

############# WINNER CHECKER #############

def winnerchecker(turn1l,turn2l):
    try:
        if "1,1" in turn1l and "1,2" in turn1l and "1,3" in turn1l:
            print xplayer,
            print "YOU HAVE WON! GG TO PLAYER 2 ;)"
            break
        elif "1,1" in turn1l and "2,1" in turn1l and "3,1" in turn1l:
            print xplayer,
            print "YOU HAVE WON! GG TO PLAYER 2 ;)"
            break
        elif turn1l == "2,1" and turn1l == "2,2" and turn1l == "2,3":
            print xplayer,
            print "YOU HAVE WON! GG TO PLAYER 2 ;)"
            break
    except:
        pass
return;

winnerchcecker(turn1l,turn2l)

程序在函数的break语句中失败,但我需要在那里休息,因为这个函数将在我的程序中使用两次来结束程序。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

You are trying to use break outside of a for or while loop. Since you are using if/elif statements, only one of these statements should ever be executed during each run of the code. Once one of the conditions evaluates to true, the rest of the conditions are skipped. So, you should be able to remove the break commands without affecting how your code works.