-4

I built a calculation and it works:

num = input("How many numbers would you like to add? ") 

list = []
for x in range(num):
    list.append(input('Number: '))

a = 0
for x in list:

    a=a+x
    print a

But when I try to make a function on of this, simply doesn't work. Can you please direct me?

list = []

def adding():
    num = input("How many numbers would you like to add? ") 

    for x in range(num):

        list.append(input('Number: '))

        a = 0
        for x in list:
            a=a+x

print adding()
erip
  • 15,290
  • 10
  • 62
  • 113
Ramon
  • 3
  • 3

3 Answers3

4

Functions without explicit returns or empty returns will return None.

>>> def foo():
...     print("Hello")
...
>>> f = foo()
Hello
>>> f is None
True

If you don't want this, use a return at the end of your function to return some value.


Some other tips.

Make your function only do one thing:

Currently your function is getting input, creating a list, and summing everything. This is a lot. You'll find that if you make your functions smaller, you'll get some nice benefits. You might consider something like this:

def prompt_for_number_of_inputs():
    return int(input("How many elements are there in the list? ")

def prompt_for_elements(num_elements):
    return [int(input("Enter a number: ")) for _ in range(num_elements)]

def sum_elements_in_list(li):
    return sum(li)

so you might use it like this:

num_elements = prompt_for_number_of_inputs()
my_list = prompt_for_elements(num_elements)
print("The sum of all the elements is {0}".format(sum_elements_in_list(my_list))

Don't shadow Python built-ins:

If you call your variables the same thing as Python builtins, you'll find yourself in trouble. See here:

>>> a = list()
>>> a
[]
>>> list = [1,2,3]
>>> a = list()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

Normally list() would create an empty list (as seen above), but this is impossible because you've bound an object to the name list. Look at other builtins which could be shadowed here.

erip
  • 15,290
  • 10
  • 62
  • 113
2

You are not returning anything.
Your indentation is incorrect.
You shouldn't use python builtins as variable names (list).
(Note: _ is often used as a disposable variable)

def adding():
    num = int(raw_input("How many numbers would you like to add? ")) 
    lst = []

    for _ in range(num):
        lst.append(int(raw_input('Number: ')))

    a = 0
    for x in lst:
        a += x
    return a

You don't really need the second loop as return sum(lst) would do the same thing.
Alternatively, you don't need lst at all:

def adding():
    num = int(raw_input("How many numbers would you like to add? ")) 

    a = 0
    for _ in range(num):
        x = int(raw_input('Number: '))
        a += x
    return a
AChampion
  • 28,166
  • 3
  • 47
  • 69
0

I changed your variable name for the list as it shadows the built-in name and added the return statement. Works as you intended it to.

sumlist = []

def adding():
    num = input("How many numbers would you like to add? ") 

    for x in range(int(num)):

        sumlist.append(int(input('Number: ')))

        a = 0
        for x in sumlist:
            a=a+x

    return a

print(adding())
Lukasz
  • 724
  • 1
  • 6
  • 20
  • You should put `sumlist` inside the function, otherwise you're playing with a global variable, which is totally preventable here. – erip Mar 27 '17 at 23:49