简体   繁体   中英

python - Break not working inside my if statement

I have just started learning my first language: python. Created a simple Tic Tac Toe game. Break is working fine for the first player but not for the second player. If Player 2 wins, It prints ( player 2 is the winner ) fine, but still gives the option for the first player to input

Here is the code:

import random

board = [0,1,2,
         3,4,5,
         6,7,8]

def show():
    print (board[0],'|',board[1],'|',board[2])
    print ('--------')
    print (board[3],'|',board[4],'|',board[5])
    print ('--------')
    print (board[6],'|',board[7],'|',board[8])
    print ('--------')



while True:

    inpt = input('select a spot player 1: ')
    inp = int(inpt)

    if board[inp] != 'x' and board[inp] != 'o':
        board[inp] = 'x'
        if (board[0] == 'x'and board[1] == 'x' and board[2] == 'x')or(board[0] == 'x'and board[3] == 'x' and board[6] == 'x')or(board[6] == 'x'and board[7] == 'x' and board[8] == 'x')or(board[2] == 'x'and board[5] == 'x' and board[8] == 'x')or(board[1] == 'x'and board[4] == 'x' and board[7] == 'x')or(board[3] == 'x'and board[4] == 'x' and board[5] == 'x')or(board[0] == 'x'and board[4] == 'x' and board[8] == 'x')or(board[2] == 'x'and board[4]=='x' and board[6] == 'x'):
            print('player 1 is the winner')
            break
        show()

        space=True                  #finding space

        while space:
            oppt = input('select a spot player 2: ')
            opp = int(oppt)

            if board[opp] != 'o' and board[opp] != 'o':
                board[opp] = 'o'
                if (board[0] == 'o' and board[1] == 'o' and board[2] == 'o')or(board[0] == 'o' and board[3] == 'o' and board[6] == 'o')or(board[6] == 'o' and board[7] == 'o' and board[8] == 'o')or(board[2] == 'o' and board[5] == 'o' and board[8] == 'o')or(board[1] == 'o' and board[4] == 'o' and board[7] == 'o')or(board[3] == 'o' and board[4] == 'o' and board[5] == 'o')or(board[0] == 'o' and board[4] == 'o' and board[8] == 'o')or(board[2] == 'o' and board[4] == 'o' and board[6] == 'o'):
                    print('player 2 is the winner')
                    break
                space = False
            else:
                print('Please check again')
    else:
        print ('PLEASE CHECK AGAIN')


    show()
brk = False
while True:

    inpt = input('select a spot player 1: ')
    inp = int(inpt)

    if board[inp] != 'x' and board[inp] != 'o':
        board[inp] = 'x'
        if (board[0] == 'x'and board[1] == 'x' and board[2] == 'x')or(board[0] == 'x'and board[3] == 'x' and board[6] == 'x')or(board[6] == 'x'and board[7] == 'x' and board[8] == 'x')or(board[2] == 'x'and board[5] == 'x' and board[8] == 'x')or(board[1] == 'x'and board[4] == 'x' and board[7] == 'x')or(board[3] == 'x'and board[4] == 'x' and board[5] == 'x')or(board[0] == 'x'and board[4] == 'x' and board[8] == 'x')or(board[2] == 'x'and board[4]=='x' and board[6] == 'x'):
            print('player 1 is the winner')
            break
        show()

        space=True                  #finding space

        while space:
            oppt = input('select a spot player 2: ')
            opp = int(oppt)

            if board[opp] != 'o' and board[opp] != 'o':
                board[opp] = 'o'
                if (board[0] == 'o' and board[1] == 'o' and board[2] == 'o')or(board[0] == 'o' and board[3] == 'o' and board[6] == 'o')or(board[6] == 'o' and board[7] == 'o' and board[8] == 'o')or(board[2] == 'o' and board[5] == 'o' and board[8] == 'o')or(board[1] == 'o' and board[4] == 'o' and board[7] == 'o')or(board[3] == 'o' and board[4] == 'o' and board[5] == 'o')or(board[0] == 'o' and board[4] == 'o' and board[8] == 'o')or(board[2] == 'o' and board[4] == 'o' and board[6] == 'o'):
                    print('player 2 is the winner')
                    break
                    brk = True
                space = False
            else:
                print('Please check again')
        if brk:
            break
    else:
        print ('PLEASE CHECK AGAIN')

now it will break out side the loop too, since it's checks i brk has been set to True after your secoond while loop, and brk is set to True when your brk condition is met

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM