QuestionQ25

VerifiedFunctions and Exceptions

What is the anticipated output from the following code?

Question Image

Explanation

An assignment to num inside a function makes it local to that function unless it is declared global or nonlocal. Evaluating num = num + 3 therefore reads the local variable before it has been assigned, which raises an UnboundLocalError.

Community Discussion

No comments yet. Be the first to start the discussion!