0

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()
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
  • 2
    Python's `break` only ever exits the innermost loop. See [this concise answer](https://stackoverflow.com/a/189685/240443). – Amadan Sep 13 '18 at 12:58
  • Also, unrelated, but this is a duplicated conditional: `if board[opp] != 'o' and board[opp] != 'o'`. You're checking the same thing, twice. – David Zemens Sep 13 '18 at 12:58
  • Thank you, Not so good with functions as of now, But if that's the only solution, will give it a try. and I have corrected the condition noticed, thanks – Vipul Khanna Sep 13 '18 at 13:09
  • You can put flag instead of break. And then condition outside innermost loop as if flag is true then return. – Sapan Zaveri Sep 13 '18 at 13:15

1 Answers1

0
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

Jonas Wolff
  • 2,034
  • 1
  • 12
  • 17