簡體   English   中英

我該如何解決:UnboundLocalError:分配前引用的局部變量“生成”

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

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

為什么它給我這個錯誤?

代碼

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)

結果

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

你知道什么是local variable嗎?


caratteri = ""創建僅存在於passwordGenerator()內部的局部變量,但是當您使用list(caratteri)時,您嘗試在generate()中使用它。

generate()中,當您使用caratteri = list(...)時,您還會創建局部變量,但是您在嘗試從caratteri獲取值之后執行此操作 - 這會給出錯誤local variable 'caratteri' referenced before assignment

更好地明確使用變量 - 將它們作為 arguments 發送

generate(lenght, caratteri) 

您可能會遇到與Password相同的問題。

您創建了全局變量Password = ""但在generate()內部創建了本地Password = "" generate()中,您可以使用global Password來處理全局Password ,而不是創建本地Password ,但更好地使用從 function 返回的值

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)

PEP 8 -- Python 代碼的樣式指南

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM