Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function Import help
#1
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?
Reply
#2
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.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding a sqrt function syntax error and <build-in function sqrt> from tkinter import Kael 0 1,855 Oct-14-2019, 05:51 PM
Last Post: Kael

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020