+ 1
Tuple as a return value in function
# This is a tuple example in python # tuple as a return value of a function import random def choice3(bucket): x = random.choice(bucket) y = random.choice(bucket) z = random.choice(bucket) return x, y, z urn = ('egg', 'spam') print(choice3(urn)) #I am having a hard time figuring out what is happening here. Can someone help me with #this please!!
3 Antworten
0
random.choice - random element of a non-empty sequence. It is random sequence between 2 items. That is function take random element from urn and return it in x, then take random from urn and return it in y and z the same.
0
Ooh okay..so you mean the bucket in the function is replaced with the contents in the urn and because we used the random function, it just takes some element from the bucket and displays it?
Am I getting it right!
0
Yes, right! for example if we have 5 elements in the urn, all of them will mixing randomly in output of function 'choice 3' so this is how it works :)