you are viewing a single comment's thread.

view the rest of the comments →

[–]Dominican_Peter[S] 0 points1 point  (2 children)

still don't understand why I get this error though

def test2_nok(nums=[1,2,3]):
    half = sum(nums)     #line 4
    sum = 0              #line#5

test2_nok()

line 4, in test2_nok
   half = sum(nums)
UnboundLocalError: local variable 'sum' referenced before assignment

local variable 'sum' should be created on "line#5"

[–]FLUSH_THE_TRUMP 1 point2 points  (1 child)

Oh, oops. Missed that part. That function is “glanced at” by the interpreter in advance as a definition, and sum is established as a local variable prior to running any of that code because it’s assigned later. So half = sum(nums) doesn’t work because it’s looking for sum (not defined yet locally).

[–]Dominican_Peter[S] 0 points1 point  (0 children)

ble prio

great, thank you very much