Alex Thomson - 5 months ago 47

Python Question

`def square_digits(num):`

result = []

if num > 0:

result.append(num%10)

num= num//10

list = ''

for i in result:

list += str(i**2)

return int(list)

This is a small function I want to square every digit of a number, just like if '991'runs through the function, '81811' will come out. But I always get wrong answer '81'.Can anyone figure out what happened?

Answer

Replace `if`

by `while`

otherwise it only computes the square of the first digit:

```
def square_digits(num):
result = []
while num > 0:
result.append(num%10)
num= num//10
list = ''
for i in result:
list += str(i**2)
return int(list)
```