3

I'm new to python and I started learning to execute function. And I started adding numbers but I could only sum two numbers and if I wanted to sum more, it would require I edit the program. Here's my code

def sum(num1,num2):
    return num1+num2

a=5
b=7
c=sum(a,b)
print (c)

Now I want to create a function to sum any amount of number you want without Editting the codes. Here was what I did:

def sum(num1,num2):
    return num1+num2
a=int(input("what is the number you want to add?: "))
ask="yes"
while(ask=="yes"):
  b=int(input("what is the number you want to add?: "))
  c=sum(a,b)
  a=c
  ask=input("do you want to add another number?: ")
else:
    c=a
    print (c)

This worked but i think there should be an easier way to do this with a function... right? Thanks for your help!

Viqtoh
  • 192
  • 1
  • 2
  • 12

5 Answers5

9

You want to have an *args parameter in your function so that you may take as many inputs more than 1:

def summ(num1, *args):
    total = num1
    for num in args:
        total = total + num
    return total

What the *args means is you can pass through as many arguments as you want:

>>> summ(1, 2, 3)
6
>>> summ(1, 2, 3, 4, 5, 6, 7)
28

args is an iterative object: we iterate through it and add each of its numbers to a total which we then return.

TerryA
  • 58,805
  • 11
  • 114
  • 143
3

you can use variable args to accept any number of arguments

def my_sum(*args):
    total = 0
    for val in args:
        total += val
    return total

You can also use python's in-built sum() to add them:

def my_sum(*args):
    return sum(args)

>>> my_sum(1,2,3,4)
10
>>> my_sum(1,5,6,7.8)
19.8
akshat
  • 1,219
  • 1
  • 8
  • 24
2

First thing to note is Python has a native sum function. Use this instead for simple calculations, don't overwrite it with your own.

But to learn more about Python, you may wish to use functools.reduce, which applies a function of two arguments cumulatively to the items of a sequence.

from functools import reduce

def mysum(*nums):
    return reduce(lambda x, y: x+y, nums)

a, b, c = 1, 2, 3

res = mysum(a,b,c)  # 6

This can be incorporated into your logic by building a list which then feeds your function:

lst = []

lst.append(int(input("what is the number you want to add?: ")))

ask = "yes"

while ask == "yes":
    lst.append(int(input("what is the number you want to add?: ")))
    ask = input("do you want to add another number?: ")
else:
    res = mysum(*lst)
    print(res)
jpp
  • 159,742
  • 34
  • 281
  • 339
0

There are several ways how to add a different quantity of numbers. First of all, you can just use a list and built-in function sum: sum([1, 2, 3])

If you wouldn't like to use a list, try to write a custom wrapper on built-in sum function (it is good practice to not override the built-in names):

def my_sum(*args):
    return sum(args)

my_sum(1, 2, 3)

Also, such usage is possible:

my_sum(2) # result is 2
my_sum()  # result is 0

But if you want a function, that takes minimum two arguments, try following:

def my_sum(num1, num2, *args):
    return num1 + num2 + sum(args)

my_sum(1, 2, 3)
Masha Kereb
  • 109
  • 1
  • 5
0

I suggest you an update based on following explanations:

  1. I recommend to avoid using the name sum for a user-defined function because this is the name of a built-in function of Python, even if it has not caused any trouble (yet) in your case
  2. You can use the sum built-in function to sum the elements of a list
  3. The else after the while is not useful
  4. You can do a = sum([a,b]) to assign directly a without using a temporary variable c.

Finally here is my suggestion of code:

a = int(input("what is the number you want to add?: "))

ask = "yes"
while (ask == "yes"):
    b = int(input("what is the number you want to add?: "))
    a = sum([a,b])
    ask = input("do you want to add another number?: ")

print (a)
Laurent H.
  • 6,316
  • 1
  • 18
  • 40