UnboundLocalError: local variable ‘xxx‘ referenced before assignment

problem

UnboundLocalError: local variable ‘xxx’ referenced before assignment

Analysis: The
variable n has been defined outside the function, and when the variable is operated inside the function, such an error will be encountered during runtime: The
core reason: mainly because the interpreter is not clear whether the variable is a global variable or a local variable.

Demonstration of error case:
Insert picture description here
re-organized code:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41475067/article/details/112603871