Python Forum
[PyGame] Function changes variable value to None
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Function changes variable value to None
#1
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?
Reply
#2
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 ''.
Reply
#3
(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?
Reply
#4
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
Reply
#5
I already solved the problem but thanks for all the suggestions
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020