#πŸ”’ debuging

75 messages Β· Page 1 of 1 (latest)

blazing belfry
wraith cloakBOT
#

@blazing belfry

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.

blazing belfry
#

plz somone help

#

The winner is None
X : O : O

: X : -

: O : X

stoic gull
#

You have three functions that test to see if there is a winner. If there is, each of them returns a value of True. What do you think happens if each does not detect a winner - and, what do you think they should return if there is not a winner?

blazing belfry
#

that there is not a winner and no one wins but when i do winn it says winner is none

stoic gull
#

Hint: When a function does not have an explicit "return" statement, by default, a function returns a value of: None.

blazing belfry
#

but where does it not say that there is a winner cause it says it in checkWin and in checkDIag

stoic gull
#

You're thinking about this backwards. Yes, if there is a winner in any of those functions, they will return a True. But what does each of the functions do when there is no winner? (And again, what would you want those functions to return?)

blazing belfry
#

wdym im realy new to this

stoic gull
#

Answer to my hint: Your code that calls these functions is looking for a Boolean value to be returned: True or False But, if there is no winner, in each of the functions, then each returns a value of None. That is not the correct value to be returned.

blazing belfry
#

so i need to add a winner function but it worcs when i go from bottom left to top right

#

The winner is X

  • : - : X

  • : X : O

X : O : O

stoic gull
#

But as you point out, that doesn't work in all situations. As an example, look at your code in checkRow:

def checkRow(board):
    global winner
    if board[0] == board[3] == board[6] and board[0] != "-":
        winner = board[0]
        return True
    elif board[1] == board[4] == board[7] and board[1] != "-":
        winner = board[1]
        return True
    elif board[2] == board[5] == board[8] and board[2] != "-":
        winner = board[2]
        return True
#

if there is no winner, what would this function return?

blazing belfry
#

return false ?

stoic gull
#

How would it do that?

blazing belfry
#

idk

stoic gull
#

Do you have any statement that tells it to return False?

blazing belfry
#

no

#

so where would that go ?

stoic gull
#

By default in Python, if you do not return anything, a function returns a value of None

blazing belfry
#

so i neeed to place a return false somewere ??

#

im realy confused

stoic gull
#

You are not confused, Yes, you need to return False in the case where it fails all test above

#

While it is implicit (not actually written), your code is doing this:

def checkRow(board):
    global winner
    if board[0] == board[3] == board[6] and board[0] != "-":
        winner = board[0]
        return True
    elif board[1] == board[4] == board[7] and board[1] != "-":
        winner = board[1]
        return True
    elif board[2] == board[5] == board[8] and board[2] != "-":
        winner = board[2]
        return True
    return None   # This is not really here, but this is what it does 
blazing belfry
#

yh but wouldnt it need to be at def checkDiag

#

not checkRow

stoic gull
#

You would have to take care of this case in all three functions.

#

Currently, in each function if each is not finding a winner, each is returning None

blazing belfry
#

so return false aswell as retern True

stoic gull
#

No, only one return would execute.

blazing belfry
#

so place return False under it in the code

#

so add both or smt

stoic gull
#

The general idea is: Check for a winning combination. If there is a winning combination then return True. If not (if it fails all the tests), then return False (note the upper case F). A function can only return from one place.

blazing belfry
#

s owhere do i add it

stoic gull
#

I should have said, only one return statement will execute.

blazing belfry
#

so i dont need all the return trues

stoic gull
#

That's not what I'm saying. If any combination is a winner, then do a "return True". If it "falls through" all your tests, then you need an explicit "return False" at the bottom.

blazing belfry
#

i have gameRunning = False at the bottom of def check TIE

#

checkTie

stoic gull
#

OK, last try. In each of your checks (diagonal, horizontal, vertical), if there is a winning combination, your code currently returns True. Good. Your code that calls those functions expects a Boolean answer of True or False. However, in each of those functions, if there is no winning combination, your code currently has no explicit return statement - so each of those functions will return a value of None. You need to change all of those functions to explicitly return False.

blazing belfry
#

so change the trues into falses

stoic gull
#

Nope. Sorry I gotta run. Please re-read what I wrote. The answers are in there.

compact canopy
#

I'll take it if you like @stoic gull

blazing belfry
#

yes plz

compact canopy
#

IrvKalb's trying to get your check* functions to return True if the check is true and False if the check fails / not true.

Presently all your checks return True if the check is true, but return None if the check fails.

Ideally a Boolean function like checkHorizontal et al should return a Boolean True/False return, not some other type.

IK's trying to fix this aspect of your code.

#

This is kind of adjacent to your actual bug, more getting the functions to be "well formed".

blazing belfry
#

wdym im new and i dont understand all of therse words and stuff

compact canopy
#

That's ok. We're all new.

safe pine
#

hey cam!

compact canopy
#

Formal logic is often called "Boolean", after George Boole, who formalised Boolean algebra. https://en.wikipedia.org/wiki/George_Boole

In Python an other language, the type which holds a true/false value is called a Boolean value for this reason. In Python that type is a bool, and has only two values: True and False.

That's the terminology here.

George Boole Jnr (; 2 November 1815 – 8 December 1864) was a largely self-taught English mathematician, philosopher and logician, most of whose short career was spent as the first professor of mathematics at Queen's College, Cork in Ireland. He worked in the fields of differential equations and algebraic logic, and is best known as the author ...

#

So, back to IK's approach....

blazing belfry
#

so i would need to add falses

#

???

compact canopy
#

In short, yes. Functions generally want to return a value of a particular type, depending on the function. A math function would usually return a float, an counting function would return an int and your checking functions should return a bool (true/false).

#

So first we fix that.

blazing belfry
#

ok and how do we do that

compact canopy
#

Functions are usually written in one of 2 styles:

  • compute something, and return a value at the end
  • do various checks and return early for various special cases, with a return at the end for when none of the checks fires ("the check is true")
#

Your check functions use "early return". Eg checkRow looks for the first row all being the same and not "-" . If so, you return True. Then you check for the second row. Etc.

What you're missing is the final:

return False
#

at the bottom for when no check fires.

blazing belfry
#

can i add you as a freind because my mum is saying i need to go to bed and i would like to finish this so i would havbe t do it otmrow

compact canopy
#

You can just post again tomorrow and ping me if nobody shows up. We're in very different timezones, so I may not be around when you return (it's morning here).

blazing belfry
#

if not it is perfectly fine

#

alr

compact canopy
#

Before you go, just this:

def checkRow(board):
    global winner
    if board[0] == board[3] == board[6] and board[0] != "-":
        winner = board[0]
        return True
    elif board[1] == board[4] == board[7] and board[1] != "-":
        winner = board[1]
        return True
    elif board[2] == board[5] == board[8] and board[2] != "-":
        winner = board[2]
        return True
    return False
blazing belfry
#

in a bit

#

πŸ™‚

compact canopy
#

See the final return False.

#

That's all IK was after.

#

Everyy Python function returns a value. If you don't provide an explicit return something, Python does an automatic return None if you reach the end of the function.

#

I think the only problem with your checkDiag is that the indices of the board cells might be wrong in your bottom left to top right test (the second one).

#

.... Hmm, but they look ok.

wraith cloakBOT
#
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.