-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
  • 16,374
  • 11
  • 66
  • 121
Ramon
  • 3
  • 3
  • 1
    1) you need to use `return` from the function 2) please do not call a list `list` since you override the function of the same name – dawg Mar 27 '17 at 20:32
  • If you want your function to return something, it needs to have a return statement. – Luke Mar 27 '17 at 20:32
  • "Does not work" is not very helpful. What doesn't work? How doesn't it work? – Andrew Li Mar 27 '17 at 20:32
  • What do you mean *"doesn't work"*? You don't `return` anything, why wouldn't it return `None`? – jonrsharpe Mar 27 '17 at 20:32
  • In Py2 `input()` is generally considered unsafe as it evaluates the input, use `int(raw_input(...))`. – AChampion Mar 27 '17 at 20:36

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
  • 16,374
  • 11
  • 66
  • 121
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
  • 29,683
  • 4
  • 59
  • 75
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())
Luke
  • 744
  • 2
  • 7
  • 23
  • 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