0
Rewriting variable with multiple functions
I know you can return a value using the return function and assign that to a variable to use later but how would I be able to return multiple values to the same variable? Ex: In an RPG for example you can have a character creater function with returns values in a list, but let's say in another function you needed to return a decreased health value to the same variable a.k.a your character from the player creater function.
2 Answers
+ 1
you can return a list, for example return [10, 20, 30], or you can return values separated by commas, return 10,20,30, if you use this second method it will return a kind of list named tuple. For example:
def test():
return 10,20
a = test()
the value of a will be (10, 20) and you can access each value as you access it in lists, like a[0], this will give 10 for you.
PS: sorry for my bad english, i'm brazilian!
+ 1
To access a returned tuple, you can also do implicit multi assignation:
def test()
return (10, 20, 30)
a, b, c = test()
The value of a will be 10, b 20 and c 30 ;)