-1

anyone could possibly help out why i've got such an error?

    def square_digits(num):
        result = [] 
        for x in num:
            result.append(x**2)
            return result
        pass

TypeError: 'int' object is not iterable

Thanks in advance

AkioD1
  • 19
  • 3

1 Answers1

0

Something like this should work. Convert the number to list of strings (digits of number) and iterate through this list.

Convert back to int when performing the square calculation (or convert to float if you are using non-int numbers)

    def square_digits(num):
        result = []
        nums = list(str(num))
        for x in nums:
            result.append(int(x)**2)
        return result
Kristof Rado
  • 713
  • 1
  • 8
  • 19