Python Forum
I created a function that generate a list but the list is empty in a new .py file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I created a function that generate a list but the list is empty in a new .py file
#1
Hi guys,

newbie to Python, pardon me if my question is subject doesn't make sense.

I created a function to generate a random list of numbers and list is called a.

def generaterandlist():
    import random

    a=[]
    b=1
    ##l = random.randint(0,10)
    ##print(l)

    for x in range(0,4):
        l = random.randint(0,10)
        a.append(l)
        
    print("These are the random list generated %s" % a)    
        
generaterandlist()
I then opens up a new .py file and calls the function which generates the list:
import generaterandlistfile
print(a)
but it says "NameError: name 'a' is not defined".
I understand the reason is I have not defined the list 'a', but I thought since 'a' is defined in the function already, and then the list was created by the function as 'a', I would expect the random number s to already created and stored in 'a'.

My intention is to use the function to create a list, and then call the function from another file and use the list to do something else..

Thanks guys,

Hope the question make sense
Reply
#2
Use this
# generaterandlistfile.py
import random

def generaterandlist():
    a=[]
    b=1
    ##l = random.randint(0,10)
    ##print(l)
 
    for x in range(0,4):
        l = random.randint(0,10)
        a.append(l)
    return a # <---- See the return statement here?
Then use it
import generaterandlistfile as glf

spam = glf.generaterandlist()
print(spam)
Reply
#3
Thanks a lot Gribouillis, exactly what I was after.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compile list of dictianories out of another list of dictianories by certain keys CatorCanulis 10 475 Jun-10-2021, 08:35 PM
Last Post: perfringo
  How to open MIDI-file and get events in a list? philipbergwerf 7 1,251 May-29-2021, 08:24 AM
Last Post: j.crater
  function that returns a list of dictionaries nostradamus64 2 229 May-06-2021, 09:58 PM
Last Post: nostradamus64
  Error on open of file created with tempfile.TemporaryDirectory() Brian177 4 507 Apr-05-2021, 07:12 PM
Last Post: Brian177
  Order a list with successive permutations based on another list yvrob 3 464 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  list.count does not appear to function Oldman45 7 765 Mar-16-2021, 04:25 PM
Last Post: Oldman45
  Saving list in a list quest_ 3 619 Mar-10-2021, 09:58 AM
Last Post: quest_
Star Convert Bytearray into List using list() Shlok 2 381 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  Function to count words in a list up to and including Sam Oldman45 13 905 Feb-18-2021, 10:22 AM
Last Post: perfringo
  What is the value after JOINING an empty list? JaneTan 2 399 Jan-04-2021, 06:25 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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