Python Forum
breaking even if conditions is not met
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
breaking even if conditions is not met
#1
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:
Reply
#2
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()
Reply
#3
also:
if gameover is True:
is bad programming style, use:
if gameover:
    ... do this ...
else:
    ... do this ...
Reply
#4
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'?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(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?

Reply
#6
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ChromeDriver breaking Python script genericusername12414 1 217 Mar-14-2024, 09:39 AM
Last Post: snippsat
  Openpyxl module breaking my code EnderSM 5 985 May-26-2023, 07:26 PM
Last Post: snippsat
  breaking out of nested loops Skaperen 3 1,174 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  How to use += after breaking for loop? Frankduc 2 1,444 Dec-31-2021, 01:23 PM
Last Post: Frankduc
  How to fix while loop breaking off raphy11574 3 2,125 Oct-12-2021, 12:56 PM
Last Post: deanhystad
  breaking a large program into functions, not acting as expected Zane217 9 2,931 Sep-18-2021, 12:37 AM
Last Post: Zane217
  Need to run 100+ Chrome’s with Selenium but can’t get past ~15 without breaking imillz 0 1,322 Sep-04-2021, 04:51 PM
Last Post: imillz
Exclamation Help in breaking bytes into bits through masking and shifting kamui123 9 4,435 Jan-11-2021, 07:42 AM
Last Post: kamui123
  breaking outof nexted loops Skaperen 4 3,061 Feb-07-2020, 02:04 AM
Last Post: Skaperen
  Breaking While Loop JustWonderful 4 3,011 Oct-28-2019, 01:12 AM
Last Post: JustWonderful

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020