为什么sum=0.0放在开头会报错?
sum = 0.0 def average(*args): if len(args) == 0: return sum for x in args: sum = sum + x return sum/len(args) print average() print average(1, 2) print average(1, 2, 2, 3, 4)
Traceback
(most recent call last):
File "index.py", line 8, in
print average()
File "index.py", line 4, in
average
return sum
UnboundLocalError: local variable 'sum' referenced before assignment
标答的sum=0.0放在def之后,为什么放在开头就会报错?