I was reading 'Starting Python' trying out in the shell some of the examples.
However, I get an error with this one. What am I doing wrong?

>>> def sum_average(*args):
    size = len(args)
    sum1 = sum(args)
    average = sum1/float(size)
    # return a tuple of three arguments
    # args is the tuple we passed to the function
    return args, sum1, average

>>> sum_average(1, 2, 3, 4, 5)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    sum_average(1, 2, 3, 4, 5)
  File "<pyshell#2>", line 3, in sum_average
    sum1 = sum(args)
  File "<pyshell#14>", line 3, in sum
    sum1 = sum(numbers)
    [...]
  File "<pyshell#14>", line 3, in sum
    sum1 = sum(numbers)
  File "<pyshell#14>", line 3, in sum
    sum1 = sum(numbers)
RuntimeError: maximum recursion depth exceeded

Thanks.

Recommended Answers

All 2 Replies

Please use an editor, hard to see what your indents are with that lousy pyshell!

Te following works fine for me. What version of Python are you using?

def sum_average(*args):
    size = len(args)
    sum1 = sum(args)
    average = sum1/float(size)
    # return a tuple of three arguments
    # args is the tuple we passed to the function
    return args, sum1, average

print sum_average(1, 2, 3, 4, 5)

## prints ((1, 2, 3, 4, 5), 15, 3.0)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.