Python 如何修复:UnboundLocalError:localvariable';生成';分配前参考
为什么它会给我这个错误 代码 结果Python 如何修复:UnboundLocalError:localvariable';生成';分配前参考,python,python-3.x,Python,Python 3.x,为什么它会给我这个错误 代码 结果 import string import random Password = "" lettere = string.ascii_letters numeri = string.digits punteggiatura = string.punctuation def getPasswordLenght(): lenght = input("How log do you want your password...\n
import string
import random
Password = ""
lettere = string.ascii_letters
numeri = string.digits
punteggiatura = string.punctuation
def getPasswordLenght():
lenght = input("How log do you want your password...\n>>> ")
return int(lenght)
def passwordGenerator(lenght):
caratteri = ""
requestPunteggiatutaIclusa = input("Punteggiatura (YES | NO)\n>>> ")
if requestPunteggiatutaIclusa.upper() == "YES" :
caratteri = f"{lettere}{numeri}{punteggiatura}"
generate(lenght)
elif requestPunteggiatutaIclusa.upper() == "NO" :
caratteri = f"{lettere}{numeri}"
generate(lenght)
else :
print("Error")
passwordGenerator(lenght)
return Password
def generate(lenght) :
caratteri = list(caratteri)
random.shuffle(caratteri)
Password = ""
for i in range(lenght):
Password = Password + caratteri[i]
i = i + 1
return Password
passwordGenerator(getPasswordLenght())
print(Password)
您要如何记录您的密码。。。
8.
Punteggiatura(是|否)
对
回溯(最近一次呼叫最后一次):
文件“/Users/paolo/Desktop/COde/PY/passwordGenerator.PY”,第46行,在
密码生成器(GetPasswordLength())
文件“/Users/paolo/Desktop/COde/PY/passwordGenerator.PY”,第33行,在passwordGenerator中
生成(长度)
文件“/Users/paolo/Desktop/COde/PY/passwordGenerator.PY”,第19行,在generate中
caratteri=列表(caratteri)
UnboundLocalError:赋值前引用的局部变量“caratteri”
你知道什么是局部变量吗
caratteri=”“
创建局部变量,该变量仅存在于passwordGenerator()
中,但在使用list(caratteri)
时,您尝试在generate()
中使用它
在generate()
中,当您使用caratteri=list(…)
时,您也会创建局部变量,但您在尝试从caratteri
中获取值后会执行此操作,这会导致在赋值之前引用的局部变量“caratteri”出现错误
更好地显式使用变量-将它们作为参数发送
How log do you want your password...
8
Punteggiatura (YES | NO)
yes
Traceback (most recent call last):
File "/Users/paolo/Desktop/COde/PY/passwordGenerator.py", line 46, in <module>
passwordGenerator(getPasswordLenght())
File "/Users/paolo/Desktop/COde/PY/passwordGenerator.py", line 33, in passwordGenerator
generate(lenght)
File "/Users/paolo/Desktop/COde/PY/passwordGenerator.py", line 19, in generate
caratteri = list(caratteri)
UnboundLocalError: local variable 'caratteri' referenced before assignment
您可能会遇到与密码相同的问题
您创建了全局变量Password=“”
,但在generate()
中您创建了本地Password=“”
。在generate()
中,您可以使用global Password
来处理全局密码
,而不是创建本地密码
,而是使用从函数返回的值
generate(lenght, caratteri)
我的版本有很多其他的变化
Password = generate(lenghtt, caratteri)
caratteri=“
创建局部变量-当您使用list(caratteri)
时,它不存在于generate()
中。在generate()。最好将其作为参数发送(显式使用)
generate(lenght,caratteri)
Password = generate(lenghtt, caratteri)
import string
import random
# --- functions ---
def ask_questions():
length = input("Password length\n>>> ")
length = int(length)
# In Linux it is popular to use upper case text in `[ ]`
# to inform user that if he press only `ENTER`
# then system will use this value as default answer.
# I inform user that `N` is default answer.
# I don't like long answers like `yes`, `no` but short `y`, n`
# and many program in Linux also use short `y`,`n`
answer = input("Use punctuations [y/N]\n>>> ")
answer = answer.upper()
characters = string.ascii_letters + string.digits
if answer == "Y":
characters += string.punctuation
elif answer != "" and answer != "N":
print("Wrong answer. I will use default settings")
return length, characters
def generate(lenght, characters):
characters = list(characters)
random.shuffle(characters)
password = characters[0:lenght]
password = ''.join(password)
return password
# --- main ---
# I seperate questions and code which generates password
# and this way I could use `generate` with answers from file or stream
length, characters = ask_questions()
password = generate(length, characters)
print(password)