user1816377
user1816377

Reputation: 245

calling to function in function

I need to build 3 funcs. the 1st is insertion sort, 2nd is generate list of random nums between 0-1 and the 3rd need to create list of randon numbers (using 2nd func.) and sort them (using 1st func.). I can't change the tests in the end and not the arguments of the funcs. I have a problem with func 3, it says NameError: global name 'my_list' is not defined. The other funcs works fine, where am I wrong in the 3rd? *the 1st func not allowed to return anything.

thanks!

my code:

def insertion_sort(lst):
    for i in range(1,len(lst)):
        current=lst[i]
        j=i-1
        while j>=0:
            if current < lst[j]:
                lst[j+1] = lst[j]
                lst[j] = current
                j-=1
            else:
                break

def random_list(n):
    import random
    my_list=[]
    for i in range(n):
        my_list.append (random.random ())
    return my_list

def sorted_random_list(n):
    random_list(n)
    insertion_sort(my_list)

### TEST FUNCTION - DON'T CHANGE THIS ####
def test_sorted(lst):
    print lst == sorted(lst) and len(lst) > 0

def test_len(lst, length):
    print len(lst)==length

def sort_and_test(lst):
    lst = lst[:]
    insertion_sort(lst)
    test_sorted(lst)

sort_and_test([13,54,3434,88,334,6,8,84,57,4,2,4,6,6])
sort_and_test(["dog","cat","cow","zebra","frog","bat","spider","monkey"])
sort_and_test(["hi","hello","how are you","what your doing"])

test_len(random_list(10), 10)
lst = sorted_random_list(10)
test_len(lst, 10)
test_sorted(lst)

Upvotes: 0

Views: 97

Answers (3)

jonhopkins
jonhopkins

Reputation: 3842

my_list is not a global variable. Because you are returning the list from random_list(), in sorted_random_list() you should have my_list = random_list(n). This will create the variable my_list within the scope of that function.

Upvotes: 1

inspectorG4dget
inspectorG4dget

Reputation: 114035

It's because you call random_list(n) but you don't assign the value it returns to any variable.

Try this instead:

my_list = random_list(n)

And that should solve your problem

Without assigning the return value of random_list() to a variable, all you're doing is computing random_variable() and throwing away the result. What you want to do is to name the result of the call to random_variable() so that you can refer to it later. This is done by assigning it to a variable with that name, in this case, my_list

Hope this helps

Upvotes: 7

Silas Ray
Silas Ray

Reputation: 26160

Well, the quickest way is to replace the code in sorted_random_list() with

new_list = random_list(n)
insertion_sort(new_list)
return new_list

but the deeper problem here appears to be a lack of understanding of scoping and the need for assignment of values to variables. my_list only exists within the scope of random_list(), so it is not available in the global namespace, which is why you are getting the error you are seeing here. You also aren't actually assigning the result of random_list(n) to anything, so you are throwing away the new list. Then you don't actually return the list created in sorted_random_list() in any case.

Upvotes: 2

Related Questions