Hi I did the following code for a Leetcode question
class Solution(object):
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
while(1):
if num in list( range(0,10) ):
return num
sum = sum( int(i) for i in str(num) )
num = sum
It yielded an error Line 11: UnboundLocalError: local variable 'sum' referenced before assignment. It was be fixed by changing variable sum to sum1.
sum is not in the list of illegal variable names (keywords) (section 2.3).
So why error? Is it that, when python sees sum = sum(...), python starts to treat sum as a variable and forget it's a function?
sum.sum? Is it a function? If so, why do you assign it ?sum()is a Python built-in fuction.