codehaus


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

returning totals in functions of math


On 2020-11-08 at 19:00:34 +0000,
Peter Pearson <pkpearson at nowhere.invalid> wrote:

> On Sun, 8 Nov 2020 13:50:19 -0500, Quentin Bock <qberz2005 at gmail.com> wrote:
> > Errors say that add takes 1 positional argument but 3 were given? Does this
> > limit how many numbers I can have or do I need other variables?
> > Here is what I have:
> > def add(numbers):
> >    total = 1
> >    for x in numbers:
> >           total += x
> >    return total
> > print(add(1999, -672, 84))
> 
> Your function "add" expects a single argument that is a list
> of numbers.  You're passing it three arguments, each a number.
> Try add([1999, -672, 84]).

Or change add to accept an arbitrary number of arguments and collect
them into a tuple:

    def add(*numbers):
        # then the rest of the function as before

BTW, why initialize total to 1?