-2

I assigned the player variable to 'x' and defined a function that swaps its values. Why does it still print 'x'?

def update_player(player):
    if player == 'x':
        return 'y'
    else:
        return 'x'


current_player = 'x'
update_player(current_player)
print(current_player)

Edit: I simply needed to assign the return value of the function to the player in order to update it.

  • 4
    You need to assign the return - `player = update_player(player)`. Or you could use player as a global variable instead of a parameter. – jonrsharpe Sep 10 '20 at 07:06

1 Answers1

1

You are printing the same variable instead of printing the function.Edit your code like this.

player = "x"
def update_player(player):
    if player == "x":
        player = "y"
    else:
        player = "x"
    return player
k=update_player(player)
print(k)