1

I have tried it in a bunch of different ways and this is one. but the idea is to define a list in one function but i cant get it to use the list in a second function. Then i also have to cube the even numbers in list 2.

def main():
    print (list1())
    print()
    print (list2())
def list1():
    list1 = []
    for i in range (100):
        list1.append(random.randint(2, 25))
    return list1

def list2():
    i = 0
    list2 = []
    for i in list1():
        if (i % 2 == 0):
            list2.append(i)

    return list2
1
  • Yes @falsetru is right. It is much safer to pass variables into a function rather than rely on the global namespace. This was functions do best. Otherwise you would have to use global list1 in list2(), which is general a bad idea. Commented Nov 19, 2016 at 9:31

1 Answer 1

2

How about declare parameters; Saving return value of list1() and pass it to list2(..) as a argument.

def main():
    lst1 = list1()
    lst2 = list2(lst1)
    print(lst1)
    print()
    print(lst2)

def list1():
    list1 = []
    for i in range (100):
        list1.append(random.randint(2, 25))
    return list1

def list2(lst1):  # accept a parameter.
    i = 0
    list2 = []
    for i in lst1:  # use the parameter, instead of calling list2()
        if i % 2 == 0:
            list2.append(i)

    return list2
Sign up to request clarification or add additional context in comments.

1 Comment

I feel like that was obvious! but nothing makes sense at 2am. Thanks a lot for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.