Python Forum

Full Version: Function changes variable value to None
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to code a Tic Tac Toe game but this function
def updateSymbols(keys, cursorPos, turn):
    if keys[K_SPACE] and list_[cursorPos] == '':
        list_[cursorPos] = turn
        if turn == 'X':
            return 'O'
        elif turn == 'O':
            return 'X'
        else:
            return ''
always changes the list_[cursorPos] to None, instead of turn.

list_ is initialized as
list_=['','','','','','','','','']
and turn as
turn = 'X'
The function is called in
turn = updateSymbols(keys, cursorPos, turn
Any solutions?
With the conditions that you are using, once a 'turn' is set to '',
it can never be set to 'X' or 'O', because you don't check for ''.
(Apr-27-2018, 10:44 AM)Larz60+ Wrote: [ -> ]With the conditions that you are using, once a 'turn' is set to '', it can never be set to 'X' or 'O', because you don't check for ''.
There's two reasons why I don't think that is the problem :
1. I never set turn to ''.
2. Even it somehow was '', there's an else statement that will return '', not None
Any other suggestions?
Show more code, There's no way to really know the answer without being able to run the code.

Good programing requires covering all cases.
Quote: I never set turn to ''.
if that is true, then remove the else statement, because it would never get executed.

Where is list_ defined?
that should create a syntax error
I already solved the problem but thanks for all the suggestions