vendredi 1 septembre 2017

Tic tac toe in Python not executing else statement

This program never enters the case for switching the variable and printing O instead always printing x.

def display_board(board):


    print('   |   |')
    print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
    print('   |   |')

def Insert_board(b,pos,var):
    global Var 
    global first 
    global Toggle
    type(pos)
    type(Var)


    if(first):

        first = False
        Toggle = False
        b.insert(pos,var)
        print(Toggle)

    else:

           if(Toggle):

                Toggle = False
                b.insert(pos,var)
                print(Toggle)
                Var='O'

           else:


                Toggle = True
                b.insert(pos,var)
                print(Toggle)
                Var='X'

def Get_Input():

    if (first ):
        global choice
        choice = input("Input enter O or X")  

    global Position 
    Position = int(input("Enter the position where you want to enter from bottom most left column  to right 0-9"))     
    #print(Position not in range(0,10))
    #if (Position not in range(0,10)) :
    #     Position = int(input("Enter the position where you want to enter from bottom most left column  to right 0-9"))
    #lse:
     #    Position = int(input("Else the position where you want to enter from bottom most left column  to right 0-9"))

Chance=9    
Toggle=True
first = True
board = [' ']*10
Var ='X'
while (Chance>0):
    global board
    global first
    global Var


    print(board)
    #display_board(board)  

    Position ='-1' 

    choice = 'X'


    Get_Input()
    #print(board)
    #print(Position)
    #print(Var)
    Insert_board(board,Position,Var)
    display_board(board)  

    #print(board)
    #print(Position)
    #print(Var)
    Chance-=1

The issue is happening with this function and it's not entering the else case:


def Insert_board(b,pos,var):
global Var 
global first 
global Toggle
type(pos)
type(Var)


if(first):

    first = False
    Toggle = False
    b.insert(pos,var)
    print(Toggle)

else:

       if(Toggle):

            Toggle = False
            b.insert(pos,var)
            print(Toggle)
            Var='O'

       else:


            Toggle = True
            b.insert(pos,var)
            print(Toggle)
            Var='X'

Aucun commentaire:

Enregistrer un commentaire