206k views
5 votes
Using your text editor, write the function sumSquares(aList). The function takes a list as an input and returns (not prints) the sum of the squares of the all the numbers which absolute value is divisible by 3. If an element in the list is not a number, the function should ignore the value and continue. When the function receives an input that is not a list, code should return the keyword None (not the string ‘None’). Hints: review the type() or isinstance() methods

1 Answer

6 votes

Answer:

We have the Python code below with appropriate comments

Step-by-step explanation:

#defining the function for use

def sumSquares(aList):

#checking if element in list is not in number as an error

if type(aList) is not list:

return 'error'

sum = 0

for x in aList:

#checking if in list and is

#divisible by 3

if type(x) in [int, float] and x % 3 == 0:

#squares of number x is x*x

#+= is an updating syntax that represents

#the sum of the squares

sum += x*x

#returning the sum, not printing

return sum

Sample test cases

print(sumSquares(5))

print(sumSquares('5'))

print(sumSquares(6.15))

print(sumSquares([1, 5, -3, 5, 9, 8, 4]))

print(sumSquares(['3', 5, -3, 5, 9.0, 8, 4, 'Hello']))

NOTE: Use in your code and view the result

User Logan Lee
by
4.6k points