TCG
TCG

Reputation: 121

Why can't I break inside an if statement?

I have this code:

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)

The program fails on the break statements in the function, but I need to have the breaks there because this function will be used twice in my program to end the program. How can I fix this?

Upvotes: 0

Views: 59

Answers (1)

jackwise
jackwise

Reputation: 324

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.

Upvotes: 1

Related Questions