zmorris
zmorris

Reputation: 35

Functions and lists

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

Upvotes: 1

Views: 51

Answers (1)

falsetru
falsetru

Reputation: 368954

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

Upvotes: 2

Related Questions