Help me out in these very easy problems


#1

Write a program to add all the element in list

num = [ 1,2,3,4,5,6]

def addition(num):

sum = 0
for i in num:
    sum +=i
    return sum

print addition(num)

write a program to multiply all the element of the list

def multiplication(num):

mul = 1
for i in num:
    mul *=i
    return mul

print multiplication(num)

Write a program to add element till this number

n = int(raw_input("Enter the number :"))
def add(n):

sum = 0
for i in range(1,n):
    sum = sum + i
    return sum

print add(n)

I am having output in all cases is (1).Where i am wrong ?


#2

In addition(num) function, indent return statement properly. It should be out of the for-loop, like so:

def addition(num):
    sum = 0
    for i in num:
        sum +=i
    return sum

Also, you've the same indentation error in the remaining two functions :slight_smile:

Hope that helps.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.