#๐Ÿ”’ tic tac toe makes me win in one move

85 messages ยท Page 1 of 1 (latest)

neat wigeon
#

i finally completed my tic tac toe project, but for some reason, after making a winner variable i win after one input. hard to explain.

stuck flameBOT
#

@neat wigeon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

neat wigeon
#

i just completed this tic tac toe project and it was working just fine. until it wasnt.

#

!pastebin

stuck flameBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

neat wigeon
#

here is the code

#

what happend is that, when a player won,it was repeating win condition, so i had to make some adjustments and then decided to make a winner variable and assigned it None. then when the player completed the horizontal vertical or diagonal layout. it wins and the program closes

#

but now, before i ever use the input to assign a position, it says the first user won.

marsh fjord
#
if check_diagonal(board) or check_horizontal(board) or check_vertical(board) != "-":

what is this line supposed to check

#

none of the functions ever return anything

neat wigeon
marsh fjord
#

do you know about function calls and function returns

neat wigeon
#

yes

#

im calling all the functions in the while loop down below

marsh fjord
#

check_diagonal(board) will always return None since you never return anything urself

#

this basically transfers to:

if None == True or None == True or None != "-":
#

and since None is not equal to "-"

#

it will pass

neat wigeon
#

no wait, i miss tolf you something

marsh fjord
#

yes, im aware of that

neat wigeon
#

damn, my bad

marsh fjord
neat wigeon
#

but whats wrong in that?

marsh fjord
#

ur never returning anything from the functions ur calling

#

so u cant compare anything to them

neat wigeon
#

you mean board[0] == None?

marsh fjord
#

!e

def function():
  pass
print(function())
if function() == "Hello":
  print("function says Hello")
stuck flameBOT
neat wigeon
#

like what i see in my code is that, if certain position of the board has "X" or "O" in it and is not "-" then it declares the winner variable as the currentplayer

marsh fjord
#

again, ur never returning anything from the functions

neat wigeon
#

mm

marsh fjord
#

so how can it equal something else than None?

#

!e

def function():
  pass
print(function())
stuck flameBOT
marsh fjord
#

when u dont return anything, the return value will always be None

#

!e

def function():
  return "-"
print(function())
stuck flameBOT
neat wigeon
#

okay, so what should be the solution?

marsh fjord
#

in the functions you are calling, if someone has won and after you set the winner, write return True

#

and change the if statement to this
if check_diagonal(board) or check_horizontal(board) or check_vertical(board):

neat wigeon
#

that still doesnt do anything

#

i changed all the functions to return True

#

and changed the if ststement

marsh fjord
#

so all ur functions look like this?

def check_horizontal(board):
        global winner
        if (board[0] == board[1] == board[2] and board[0] != "-") or (board[3] == board[4] == board[5] and board[3] != 3) or (board[6] == board[7] == board[8] and board[0] != "-"):
           winner = currentplayer           
           return True
    
def check_vertical(board):
        global winner
        if (board[0] == board[3] == board[6] and board[0] != "-") or (board[1] == board[4] == board[7] and board[1] != "-") or (board[2] == board[5] == board[8] and board[0]) != "-":
            winner = currentplayer
            return True
            

def check_diagonal(board):
        global winner
        if (board[0] == board[4] == board[8] and board[0] != "-") and (board[0] or board[6] == board[4] == board[5] and board[2] != "-"):
            winner = currentplayer
            return True
neat wigeon
#

yes

#

!pastebin

stuck flameBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

neat wigeon
#

let me show you

#

here

marsh fjord
#
def check_horizontal(board):
        global winner
        if (board[0] == board[1] == board[2] and board[0] != "-") or (board[3] == board[4] == board[5] and board[3] != 3) or (board[6] == board[7] == board[8] and board[0] != "-"):
           winner = currentplayer
           return True 

look over this function real quick

#

do you see anything off?

neat wigeon
#

yeah

#

errors

#

board[3] != 3 and board[0] != "-" where it shouldnt be

marsh fjord
#

yes

#

all of your check_ functions has errors

#

look through all of those

#

(check_horizontal, check_vertical, check_diagonal)

neat wigeon
#

IT WORKKKSSSSSSSSSSSSSSSSSSSSSSSSSS

#

OMG

#

the errors made the wole function shit in just one command hunh

#

wow

#

wait

#

i removed the erros, but something isnt right

marsh fjord
#

what is it

neat wigeon
#

can you run this one time and tell me why i am not able to put an icon on the 3rd row?

#

like when i put "X" anywhere on the third row it just declares win

marsh fjord
#
if (board[0] == board[4] == board[8] and board[0] != "-") and (board[0] or board[6] == board[4] == board[5] and board[2] != "-"):
#

theres something wrong here

neat wigeon
#

ah, i see it

#

bruh

#

can you look it one last time please

#

๐Ÿ˜ญ

#

it still hasnt changed

#

umm

#

@marsh fjord you there?

#

๐Ÿ˜ญ

#

!close

stuck flameBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.