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