Hi all (my first post 🙂 )
Why am getting different results for the individual components of function results as opposed to the set of them?
import random
def test():
di1 = random.randint(1, 6)
di2 = random.randint(1, 6)
diTotal = di1+di2
return diTotal, di1, di2
print(test()[0], test()[1], test()[2], test())
>Solution :
Each time you call your function, two new random numbers are produced. You call the test function 4 times in print(test()[0], test()[1], test()[2], test()). So, 4 pairs of random numbers are produced, and they are different.
If you call the test function just once, you’ll have the same result:
import random
def test():
di1 = random.randint(1, 6)
di2 = random.randint(1, 6)
diTotal = di1+di2
return diTotal, di1, di2
test_output = test()
print(test_output[0], test_output[1], test_output[2], test_output)
The output:
(3, 1, 2, (3, 1, 2))