Python-local variable 'raw_password' referenced before assignment

此页面是否是列表页或首页?未找到合适正文内容。

Python-local variable 'raw_password' referenced before assignment

标签:elsecalint作用域beforesign作用方案why

where?

  执行Python程序的时候,报这个错

why?

  变量作用域问题,在分支中定义的变量,当满足条件的时候则可以正确得到变量,当不满足条件的时候则报这个错

way?

  把变量从分支中抽离到分支上面,或者在另外分支都定义这个变量,让其一直到访问都定义过

错误代码

def show_error(number):
# 当满足这个条件的时候,result可以正确得到,当不满足时候则得不到result变量
if number <= 10:
result = number
print(result)

if __name__ == ‘__main__‘:
for i in range(20):
show_error(i)

正确代码

def show_error(number):
# 解决方案1
result = -1
if number <= 10:
result = number
# 解决方案2
# else:
# result = -1
print(result)

if __name__ == ‘__main__‘:
for i in range(20):
show_error(i)

  

Python-local variable 'raw_password' referenced before assignment

标签:elsecalint作用域beforesign作用方案why

原文地址:https://www.cnblogs.com/2bjiujiu/p/9053685.html

作者: 大白菜装机

为您推荐

返回顶部