Galih
Galih

Reputation: 45

Taking a list out of a function in Python

Why do I can't take out a list I made from a function into the main program?

It was successful when I printed it inside the function, but when I tried to do it on the main program, it was said "list_numb is not defined"

Been trying to look for the bugs but didn't help..

Thanks and please help!

def inputlist(listlong):
    i = 0
    list_numb = []
    while i < listlong: 
        list_numb.append(int(input("data ke-"+str(i)+": ")))
        i+=1
    return list_numb

inputlist(int(input("How many data do you wanna put in? ")))
print(list_numb)

Upvotes: 0

Views: 87

Answers (4)

Colin Hancey
Colin Hancey

Reputation: 229

You've created the list (a variable) inside your function, so it only exists there. Returning the variable does not put that variable into your main program.

I think you want to do this:

print(inputlist(listlong))

It will print whatever the function returns, which in this case, is list_numb


Alternatively, you can define list_numb outside of your function (and in the main program), and then edit that value inside your function. You'll need one extra line to tell your function that you want to edit the list_numb that now exists in your main program, and not a list_numb that exists only inside the function. You also won't need to return anything.

For example,

list_numb = []

def inputlist(listlong):
    global list_numb  

    i = 0
    list_numb = []
    while i < listlong: 
        list_numb.append(int(input("data ke-"+str(i)+": ")))
        i+=1

inputlist(int(input("How many data do you wanna put in? ")))
print(list_numb)

Upvotes: 0

bhansa
bhansa

Reputation: 7504

You are returning the list from your function but not using it, and it is nowhere defined in your code other than function. Either you can define it globally in your program like this:

def inputlist(listlong):
    i = 0
    while i < listlong: 
        list_numb.append(int(input("data ke-"+str(i)+": ")))
        i+=1
    return list_numb

list_numb = []
inputlist(int(input("How many data do you wanna put in? ")))
print(list_numb)

Or just use the returned list from your function like:

list_numb = inputlist(int(input("How many data do you wanna put in? ")))

You can also reduce your code length:

list_numb = [input("data ke-"+str(i)) for i in range(int(input("How many data do you wanna put in? ")))]
print(list_numb)

Upvotes: 0

Rohit-Pandey
Rohit-Pandey

Reputation: 2159

You can also run this program like this:

def inputlist(listlong):
    i = 0
    list_numb = []
    while i < listlong: 
        list_numb.append(int(input("data ke-"+str(i)+": ")))
        i+=1
    return list_numb

print(inputlist(int(input("How many data do you wanna put in? "))))

Upvotes: 0

Daniyal Ahmed
Daniyal Ahmed

Reputation: 755

You are basically returning the list so you need to save it in some variable so the program should be:

 def inputlist(listlong):
    i = 0
    list_numb = []
    while i < listlong: 
        list_numb.append(int(input("data ke-"+str(i)+": ")))
        i+=1
    return list_numb

list_numb=inputlist(int(input("How many data do you wanna put in? ")))
print(list_numb)

Upvotes: 5

Related Questions