Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tuple cannot be interpreted as an integer error, how to cycle through the numbers in a tuple?

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

So 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?)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading