[SOLVED] How can I store a result of a function in a variable instead of just printing it?

Issue

I’m trying to store the result of random.random into a variable like so:

import  random

def randombb():
    a = random.random()
    print(a)
 
randombb()

How can I properly get a?

Solution

Generally, you return the value. Once you’ve done that, you can assign a name to the return value just like anything else:

def randombb():
    return random.random()

a = randombb()

As a side note — The python tutorial is really great for anyone looking to learn a thing or two about the basics of python. I’d highly recommend you check it out. Here is the section on functions: https://docs.python.org/2/tutorial/controlflow.html#defining-functions

Answered By – mgilson

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published.