简体   繁体   English

我该如何解决:UnboundLocalError:分配前引用的局部变量“生成”

[英]how can i fix: UnboundLocalError: local variable 'generate' referenced before assignment

Error:  UnboundLocalError: local variable 'generate' referenced before assignment

why it's giving me this error?为什么它给我这个错误?

Code代码

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)

Result结果

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

Do you know what is local variable ?你知道什么是local variable吗?


caratteri = "" creates local variable which exists only inside passwordGenerator() but you try to use it in generate() when you use list(caratteri) . caratteri = ""创建仅存在于passwordGenerator()内部的局部变量,但是当您使用list(caratteri)时,您尝试在generate()中使用它。

In generate() you create also local variable when you use caratteri = list(...) but you do this after trying to get value from caratteri - and this gives error local variable 'caratteri' referenced before assignment .generate()中,当您使用caratteri = list(...)时,您还会创建局部变量,但是您在尝试从caratteri获取值之后执行此操作 - 这会给出错误local variable 'caratteri' referenced before assignment

Better use variables explicitly - send them as arguments更好地明确使用变量 - 将它们作为 arguments 发送

generate(lenght, caratteri) 

And the same problem you may have with Password .您可能会遇到与Password相同的问题。

You created global variable Password = "" but inside generate() you create local Password = "" .您创建了全局变量Password = ""但在generate()内部创建了本地Password = "" In generate() you could use global Password to work with global Password instead of creating local Password but beetter use value which you return from functiongenerate()中,您可以使用global Password来处理全局Password ,而不是创建本地Password ,但更好地使用从 function 返回的值

Password = generate(lenghtt, caratteri)

My version with many other changes我的版本有许多其他变化

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)

PEP 8 -- StyleGuide for Python Code PEP 8 -- Python 代码的样式指南

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复“UnboundLocalError:分配前引用的局部变量'user_score'”? - How can I fix "UnboundLocalError: local variable 'user_score' referenced before assignment"? 如何解决此错误“UnboundLocalError:分配前引用的局部变量'a'” - How can I fix this error “UnboundLocalError: local variable 'a' referenced before assignment” 如何修复“UnboundLocalError:赋值前引用的局部变量‘books’”? - How to fix "UnboundLocalError: local variable 'books' referenced before assignment"? 如何修复 UnboundLocalError:在 Python 中分配之前引用的局部变量“df” - How to fix UnboundLocalError: local variable 'df' referenced before assignment in Python 如何修复 UnboundLocalError:赋值前引用的局部变量“o1” - How to fix UnboundLocalError: local variable 'o1' referenced before assignment UnboundLocalError:赋值前引用了局部变量“i” - UnboundLocalError: local variable 'i' referenced before assignment 赋值之前引用了unboundlocalerror局部变量&#39;i&#39; - unboundlocalerror local variable 'i' referenced before assignment 如何修复我的代码中的“UnboundLocalError:分配前引用的局部变量“dc”? - How do I fix 'UnboundLocalError: local variable 'dc' referenced before assignment' in my code? 如何修复 UnboundLocalError:分配前引用的局部变量“命令” - How do i fix UnboundLocalError: local variable 'command' referenced before assignment 如何在没有全局变量的情况下解决“ UnboundLocalError:赋值之前引用了本地变量&#39;foo&#39;”错误? - How can I get around “UnboundLocalError: local variable 'foo' referenced before assignment” errors without global variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM