Copacetic11
Copacetic11

Reputation: 3

Why does my variable (winning) not get reassigned when I try to reassign it?

def winningcheck(board):

    winning == ''
    
    if board[1] == board[2] == board[3]: 
        
        print('The game has been won')
        
        winning == 'True'
        
    else:
        print('The game has not been won')
        

test = ['#', 'X ', 'X ', 'X ', ' ', ' ', ' ', ' ', ' ', ' ']

winningcheck(test)

print(winning)

I'm a beginner, but I expected the variable winning to be reassigned to 'true' when the 'if' condition was met. However, it just returns an empty string instead when I try to print the variable.

Upvotes: -1

Views: 48

Answers (2)

Yep Yep
Yep Yep

Reputation: 530

You did winning== instead of winning='True', this was a condition and not an assignment.
Additionally you need to make winning a global variable as I've shown. otherwise the code outisde the function can't access it.

winning = ''
def winningcheck(board):
    global winning
    if board[1] == board[2] == board[3]: 
        
        print('The game has been won')
        
        winning = 'True'
        
    else:
        print('The game has not been won')
        

test = ['#', 'X ', 'X ', 'X ', ' ', ' ', ' ', ' ', ' ', ' ']

winningcheck(test)

print(winning)

But you should use a boolean variable for winning instead of a string, as its more efficient and it serves the intended use for you.
So you would init winning = False and change with winning = True.

Upvotes: 1

Keilo
Keilo

Reputation: 986

Comparison:

winning == 'True'

Assignment:

winning = True

Note the different number of "=".

Upvotes: 0

Related Questions