Python Forum

Full Version: Function Import help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am writing a code for homework, this isn't my code but the error is very similar:
import random

def main():
    random()
    print(t)

def random():
    import random
    t = random.randint(1,100)
    return t   

main()
Why doesn't the 't' value return and get printed? When I run it, I get an error saying it's undefined. How can I fix this?
t is returned from random(), but it isn't assigned to any variable. As compared to, for example, your line #9.
print(t) on line #5 doesn't know what t is.
variable t in def random is local variable (i.e. the scope in which it is visible/exists is function random).
As j.carter said you call random() on line 4, but ypu don't assign what it return to a variable to use it later on.