breaking even if conditions is not met - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: breaking even if conditions is not met (/thread-15837.html) |
breaking even if conditions is not met - Naito - Feb-02-2019 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() the break statement triggers even if the conditions are not met (if gameover is True)thank you! here is the full code: RE: breaking even if conditions is not met - danielsvens - Feb-02-2019 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() RE: breaking even if conditions is not met - Larz60+ - Feb-02-2019 also: if gameover is True:is bad programming style, use: if gameover: ... do this ... else: ... do this ... RE: breaking even if conditions is not met - perfringo - Feb-02-2019 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'? RE: breaking even if conditions is not met - Naito - Feb-03-2019 (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: thanks ill keep that in mind (Feb-02-2019, 06:06 PM)danielsvens Wrote: Hi, thanks for your reply!, i have a question why you removed the variable gamemode in win()?, and just returned True if you won? so i did remove the try funci did define the variable in win() "global gameover" did i miss something? or just this is a bad programming style? RE: breaking even if conditions is not met - perfringo - Feb-03-2019 (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. |