Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am i getting a result for the components of my return values as opposed to the complete set of return values

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading