Python assignment return value

Question:

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.

Asked By: Mattan Elkaim

||

Answers:

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)
Answered By: Anudeep Kosuri
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.