-1
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.

2 Answers2

1

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.

S H
  • 415
  • 3
  • 14
  • In your example you need to declare ```winning``` as a global variable inside your function as well. Otherwise you create a local ```winning``` variable inside ```winningcheck``` shadowing the global scope variable when assigning a new value – Wolric Jul 23 '23 at 11:00
  • True, I'm used to c++ rather than python so i forgot :). Thanks for reminding me! – S H Jul 23 '23 at 11:05
0

Comparison:

winning == 'True'

Assignment:

winning = True

Note the different number of "=".

Keilo
  • 963
  • 1
  • 7
  • 13