-2
def sum_all(*num):
    for number in range(num):
        sq = pow(number,2)
        total = sum(sq)
    return total

I am trying to write a function that can take arbitrary arguments and return the sum of the squares of the argument...not sure how to cycle through the integer values in a tuple (also if the arguments were floats with decimals how would the code change?)

MichaelCG8
  • 579
  • 2
  • 14
  • `num` is a confusing name as it is a tuple of numbers. Maybe `nums` would make a bit more sense. And do iterate a tuple you just do `for number in nums`, you can't pass a tuple to `range`... – Tomerikoo May 31 '22 at 14:26
  • Does this answer your question? [What does \*\* (double star/asterisk) and \* (star/asterisk) do for parameters?](https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters) – Chris May 31 '22 at 17:27

1 Answers1

0

When passing arguments using * the argument is a tuple, which contains all the values. I've changed the name to be pluralized.

def sum_all(*nums):
    total = 0
    for number in nums:
        sq = pow(number,2)
        total += sq
    return total

or more concisely

def sum_all(*nums):
    return sum(number**2 for number in nums)
MichaelCG8
  • 579
  • 2
  • 14