Tuple as a return value in function | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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!!

5th Dec 2017, 9:31 PM
Divya Tiruvayipati
Divya Tiruvayipati - avatar
3 Réponses
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.
6th Dec 2017, 2:02 AM
Oleksandr Rubtsov
Oleksandr Rubtsov - avatar
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!
7th Dec 2017, 8:18 PM
Divya Tiruvayipati
Divya Tiruvayipati - avatar
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 :)
7th Dec 2017, 11:13 PM
Oleksandr Rubtsov
Oleksandr Rubtsov - avatar