Python Forum

Full Version: breaking even if conditions is not met
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello, i am making a tic tac toe game for learning purposes
def win():
    global gameover
    gameover = False
    if first is True:
        win1 = board[0] == x and board[1] == x and board[2] == x
        win2 = board[3] == x and board[4] == x and board[5] == x
        win3 = board[6] == x and board[7] == x and board[8] == x
        win4 = board[0] == x and board[3] == x and board[6] == x
        win5 = board[1] == x and board[4] == x and board[7] == x
        win6 = board[2] == x and board[5] == x and board[8] == x
        win7 = board[0] == x and board[4] == x and board[8] == x
        win8 = board[2] == x and board[4] == x and board[6] == x
        if (win1 or win2 or win3 or win4 or win5 or win6 or win7 or win8) is True:
            print("You won!")
            gameover = True
            return gameover
    if first is not True:
        wino1 = board[0] == o and board[1] == o and board[2] == o
        wino2 = board[3] == o and board[4] == o and board[5] == o
        wino3 = board[6] == o and board[7] == o and board[8] == o
        wino4 = board[0] == o and board[3] == o and board[6] == o
        wino5 = board[1] == o and board[4] == o and board[7] == o
        wino6 = board[2] == o and board[5] == o and board[8] == o
        wino7 = board[0] == o and board[4] == o and board[8] == o
        wino8 = board[2] == o and board[4] == o and board[6] == o
        if (wino1 or wino2 or wino3 or wino4 or wino5 or wino6 or wino7 or wino8) is True:
            print("You won!")
            gameover = True
            return gameover


def game():
    print("welcome to tic-tac-toe")
    chosexo()
    while True:
        if gameover is True:
            break
        if first is True:
            win()
            appenders()
            theboard()
        if first is False:
            win()
            theboard()
            appenders()
Error:
welcome to tic-tac-toe choose X or O: x You start first Process finished with exit code 0
the break statement triggers even if the conditions are not met (if gameover is True)
thank you!
here is the full code:
Hi,

You have not declared the gameover variable. if you remove the try except at the end you should get an error message that tells you what is wrong.


if first:
this piece of code is equivelant to "if first is True:"

I have modified some of your code so you get the general idea.
also if you are going to check if first is true or false you can use if, else statement instead of if true if false

def win():
    if first is True:
        win1 = board[0] == x and board[1] == x and board[2] == x
        win2 = board[3] == x and board[4] == x and board[5] == x
        win3 = board[6] == x and board[7] == x and board[8] == x
        win4 = board[0] == x and board[3] == x and board[6] == x
        win5 = board[1] == x and board[4] == x and board[7] == x
        win6 = board[2] == x and board[5] == x and board[8] == x
        win7 = board[0] == x and board[4] == x and board[8] == x
        win8 = board[2] == x and board[4] == x and board[6] == x
        if (win1 or win2 or win3 or win4 or win5 or win6 or win7 or win8) is True:
            print("You won!")
            return True
        else:
            return False
    else:
        wino1 = board[0] == o and board[1] == o and board[2] == o
        wino2 = board[3] == o and board[4] == o and board[5] == o
        wino3 = board[6] == o and board[7] == o and board[8] == o
        wino4 = board[0] == o and board[3] == o and board[6] == o
        wino5 = board[1] == o and board[4] == o and board[7] == o
        wino6 = board[2] == o and board[5] == o and board[8] == o
        wino7 = board[0] == o and board[4] == o and board[8] == o
        wino8 = board[2] == o and board[4] == o and board[6] == o
        if (wino1 or wino2 or wino3 or wino4 or wino5 or wino6 or wino7 or wino8) is True:
            print("You won!")
            return True
        else:
            return False


def game():
    print("welcome to tic-tac-toe")
    chosexo()
    while True:
        appenders()
        theboard()
        gameover = win()

        if gameover:
            break

game()
also:
if gameover is True:
is bad programming style, use:
if gameover:
    ... do this ...
else:
    ... do this ...
I just observe that in function win() there is no 'first' defined and it should give NameError. Same applies to x and o which are also not defined. Shouldn't these be 'x' and 'o'?
(Feb-02-2019, 11:40 PM)perfringo Wrote: [ -> ]I just observe that in function win() there is no 'first' defined and it should give NameError. Same applies to x and o which are also not defined. Shouldn't these be 'x' and 'o'?

hi, i have defined all the variables check the spoilers thanks

(Feb-02-2019, 10:03 PM)Larz60+ Wrote: [ -> ]also:
if gameover is True:
is bad programming style, use:
if gameover:
    ... do this ...
else:
    ... do this ...

thanks ill keep that in mind

(Feb-02-2019, 06:06 PM)danielsvens Wrote: [ -> ]Hi,

You have not declared the gameover variable. if you remove the try except at the end you should get an error message that tells you what is wrong.


if first:
this piece of code is equivelant to "if first is True:"

I have modified some of your code so you get the general idea.
also if you are going to check if first is true or false you can use if, else statement instead of if true if false

def win():
    if first is True:
        win1 = board[0] == x and board[1] == x and board[2] == x
        win2 = board[3] == x and board[4] == x and board[5] == x
        win3 = board[6] == x and board[7] == x and board[8] == x
        win4 = board[0] == x and board[3] == x and board[6] == x
        win5 = board[1] == x and board[4] == x and board[7] == x
        win6 = board[2] == x and board[5] == x and board[8] == x
        win7 = board[0] == x and board[4] == x and board[8] == x
        win8 = board[2] == x and board[4] == x and board[6] == x
        if (win1 or win2 or win3 or win4 or win5 or win6 or win7 or win8) is True:
            print("You won!")
            return True
        else:
            return False
    else:
        wino1 = board[0] == o and board[1] == o and board[2] == o
        wino2 = board[3] == o and board[4] == o and board[5] == o
        wino3 = board[6] == o and board[7] == o and board[8] == o
        wino4 = board[0] == o and board[3] == o and board[6] == o
        wino5 = board[1] == o and board[4] == o and board[7] == o
        wino6 = board[2] == o and board[5] == o and board[8] == o
        wino7 = board[0] == o and board[4] == o and board[8] == o
        wino8 = board[2] == o and board[4] == o and board[6] == o
        if (wino1 or wino2 or wino3 or wino4 or wino5 or wino6 or wino7 or wino8) is True:
            print("You won!")
            return True
        else:
            return False


def game():
    print("welcome to tic-tac-toe")
    chosexo()
    while True:
        appenders()
        theboard()
        gameover = win()

        if gameover:
            break

game()

thanks for your reply!, i have a question why you removed the variable gamemode in win()?, and just returned True if you won?

Error:
Traceback (most recent call last): File "D:/Python/Medi/Medi.py", line 105, in <module> game() File "D:/Python/Medi/Medi.py", line 93, in game if gameover is True: NameError: name 'gameover' is not defined
so i did remove the try func
i did define the variable in win() "global gameover" did i miss something? or just this is a bad programming style?

(Feb-03-2019, 10:55 AM)Naito Wrote: [ -> ]hi, i have defined all the variables check the spoilers thanks

From spoiler I found keywords learning and AI. In learning process you could have a look at tic-tac-toe Minimax just to get an idea how others approach this.