Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 breaking even if conditions is not met Naito Silly Frenchman Posts: 25 Threads: 11 Joined: Jan 2019 Reputation: 1 Likes received: 2 #1 Feb-02-2019, 04:29 PM (This post was last modified: Feb-02-2019, 04:30 PM by Naito. Edited 5 times in total.) 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: Hide/Show```import random # the tic tac toe project, (for LEARNING purposes) # AI overtakes your signs # when you make a fault while loop continues (AI makes another move) x = "X" o = "O" board = list(range(10)) def chosexo(): global first xoro = input("choose X or O: ").lower() first = False if xoro == "x": first = True # player starts first print("You start first") return first if xoro == "o": first = False # player starts second print("AI starts first") return first else: raise Exception("Please choose X or O") def appenders(): global userplay userplay = int(input("pick from 1 to 9 to draw the shape you chose")) if 1 <= userplay <= 9: if first is True: if board[userplay - 1] != x and board[userplay - 1] != o: board[userplay - 1] = x else: return print("There is already a sign there!") else: if board[userplay - 1] != x and board[userplay - 1] != o: board[userplay - 1] = o else: return print("There is already a sign there!") else: print("Please pick a number from 1 to 9") def theboard(): print("|------------------|----------------|-------------------|") print("| 1 | 2 | 3 |") print("|", board[0], "|", board[1], '|', board[2], '|') print("|------------------|----------------|-------------------|") print("| 4 | 5 | 6 |") print("|", board[3], "|", board[4], '|', board[5], '|') print("|------------------|----------------|-------------------|") print("| 7 | 8 | 9 |") print("|", board[6], "|", board[7], '|', board[8], '|') print("|------------------|----------------|-------------------|") 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() try: game() except: Exception('Something went wrong!') ``` danielsvens Unladen Swallow Posts: 3 Threads: 0 Joined: Feb 2019 Reputation: 0 Likes received: 0 #2 Feb-02-2019, 06:06 PM (This post was last modified: Feb-02-2019, 06:06 PM by danielsvens. Edited 1 time in total.) 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() ``` Larz60+ aetate et sapientia Posts: 7,396 Threads: 287 Joined: Sep 2016 Reputation: 264 Likes received: 1035 #3 Feb-02-2019, 10:03 PM also: `if gameover is True:`is bad programming style, use: ```if gameover: ... do this ... else: ... do this ... ``` perfringo Verb Conjugator Posts: 721 Threads: 1 Joined: Jun 2018 Reputation: 64 Likes received: 143 #4 Feb-02-2019, 11:40 PM 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 Life of Brian: Conjugate the verb, "to go" ! Naito Silly Frenchman Posts: 25 Threads: 11 Joined: Jan 2019 Reputation: 1 Likes received: 2 #5 Feb-03-2019, 10:55 AM (This post was last modified: Feb-03-2019, 10:55 AM by Naito. Edited 1 time in total.) (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 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? perfringo Verb Conjugator Posts: 721 Threads: 1 Joined: Jun 2018 Reputation: 64 Likes received: 143 #6 Feb-03-2019, 12:30 PM (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. Naito likes this post 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 Life of Brian: Conjugate the verb, "to go" ! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Breaking subprocess loop from parent process kapibara 2 75 9 hours ago Last Post: Gribouillis How to define Missing value on some certain conditions? FreddieTheCat 3 385 Jan-14-2019, 01:35 AM Last Post: scidam If conditions with time limit unknowntothem 4 445 Nov-09-2018, 08:59 PM Last Post: nilamo 3 conditions to check and return ilcaa72 3 687 May-24-2018, 03:13 AM Last Post: ilcaa72 Fill as list (with conditions). EmericCrue 6 1,081 May-11-2018, 03:21 PM Last Post: nilamo for loop with 2 conditions vaison 9 1,395 Apr-19-2018, 01:28 PM Last Post: vaison How do you check if something exists or doesn't in the conditions of an if? Klar 1 678 Dec-15-2017, 03:47 PM Last Post: sparkz_alot Help with multiple conditions on ATM program jakegold98 6 2,962 Dec-06-2017, 05:18 PM Last Post: jakegold98 applying 2 conditions to a loop Pedroski55 1 801 Nov-08-2017, 07:11 PM Last Post: nilamo Simplifying multiple "or" conditions in if statement. rhubarbpieguy 8 33,635 Jul-22-2017, 12:19 PM Last Post: rhubarbpieguy

Forum Jump:

Users browsing this thread: 1 Guest(s)