Python错误:UnboundLocalError: local variable 'max' referenced before assignment

Assigned before the reference a local variable "max"
Here Insert Picture DescriptionHere Insert Picture Description
after the internal variable assignment function to modify the variable it will be regarded as Python interpreter local variable instead of global variables, the program execution when the max = max (nums [: k ]) when want to perform max is considered local variables defined within the function can not find the max of the natural error
Solutions: Inside the function, to add global variable modifier, it is declared as a global variable

However, I added a global max is still wrong after
Here Insert Picture Description
the original variable and function names max () the same name
so the max variable name changed to maxa
Here Insert Picture Description

Released four original articles · won praise 1 · views 261

Guess you like

Origin blog.csdn.net/weixin_45729335/article/details/104697463