原因 在内层结构中使用了外层变量,如 i = 0 #外层结构 def do(): #内层结构 i = i + 1 #直接使用会报错 解决 在内层结构中,如果要使用外层变量,需要先用global修饰: i =