#π debuging
75 messages Β· Page 1 of 1 (latest)
@blazing belfry
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.
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?
that there is not a winner and no one wins but when i do winn it says winner is none
Hint: When a function does not have an explicit "return" statement, by default, a function returns a value of: None.
but where does it not say that there is a winner cause it says it in checkWin and in checkDIag
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?)
wdym im realy new to this
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.
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
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?
return false ?
How would it do that?
idk
Do you have any statement that tells it to return False?
By default in Python, if you do not return anything, a function returns a value of None
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
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
so return false aswell as retern True
No, only one return would execute.
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.
s owhere do i add it
I should have said, only one return statement will execute.
so i dont need all the return trues
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.
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.
so change the trues into falses
Nope. Sorry I gotta run. Please re-read what I wrote. The answers are in there.
I'll take it if you like @stoic gull
yes plz
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".
wdym im new and i dont understand all of therse words and stuff
That's ok. We're all new.
hey cam!
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....
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.
ok and how do we do that
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.
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
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).
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
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.
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.