Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法弄清楚为什么会出现UnboundLocalError_Python_Python 3.x_Function_Exception - Fatal编程技术网

Python 无法弄清楚为什么会出现UnboundLocalError

Python 无法弄清楚为什么会出现UnboundLocalError,python,python-3.x,function,exception,Python,Python 3.x,Function,Exception,我试图让代码“除了”一个整数字符串-“长度”。当我输入Exception ValueError时,返回“请输入一个数字”,但添加了更多错误。我还添加了Exception UnboundLocalError,但这似乎不起作用。请让我知道我做错了什么!这是我的密码: import random import string def RPG(): try: RPG = "" count = 0 length = int(

我试图让代码“除了”一个整数字符串-“长度”。当我输入Exception ValueError时,返回“请输入一个数字”,但添加了更多错误。我还添加了Exception UnboundLocalError,但这似乎不起作用。请让我知道我做错了什么!这是我的密码:

import random
import string


def RPG():
    try:
        RPG = ""
        count = 0
        length = int(
            input("How many characters would you like in your password? "))
    except (ValueError, UnboundLocalError):
        print("Please enter a number.")
    while count != length:
        upper = [random.choice(string.ascii_uppercase)]
        lower = [random.choice(string.ascii_lowercase)]
        num = [random.choice(string.digits)]
        symbol = [random.choice(string.punctuation)]
        everything = upper + lower + num + symbol
        RPG += random.choice(everything)
        count += 1
        continue
    if count == length:
        print(RPG)
# could also use string.printable for digits, letters, punct., and whitespace.


RPG()
以下是我从使用此代码并在长度中键入字符串而不是整数中得到的信息:

How many characters would you like in your password? j
Please enter a number.
Traceback (most recent call last):
  File "c:\Users\jkelly\Desktop\python\code.py", line 28, in <module>
    pwd()
  File "c:\Users\jkelly\Desktop\python\code.py", line 14, in pwd
    while count != length:
UnboundLocalError: local variable 'length' referenced before assignment
您希望密码中包含多少个字符?J
请输入一个数字。
回溯(最近一次呼叫最后一次):
文件“c:\Users\jkelly\Desktop\python\code.py”,第28行,在
pwd()
文件“c:\Users\jkelly\Desktop\python\code.py”,第14行,在pwd中
一边数数!=长度:
UnboundLocalError:赋值前引用的局部变量“length”

我只希望“请输入一个数字”,而不是其余的错误,任何帮助将不胜感激。谢谢你抽出时间

如果导致错误,程序的其余部分仍将执行。您需要重复输入,直到获得正确的输入

import random
import string


def RPG():
    while True:
        try:
            RPG = ""
            count = 0
            length = int(
                input("How many characters would you like in your password? "))
            break
        except (ValueError, UnboundLocalError):
            print("Please enter a number.")
    while count != length:
        upper = [random.choice(string.ascii_uppercase)]
        lower = [random.choice(string.ascii_lowercase)]
        num = [random.choice(string.digits)]
        symbol = [random.choice(string.punctuation)]
        everything = upper + lower + num + symbol
        RPG += random.choice(everything)
        count += 1
        continue
    if count == length:
        print(RPG)
# could also use string.printable for digits, letters, punct., and whitespace.


RPG()

如果导致错误,程序的其余部分仍将执行。您需要重复输入,直到获得正确的输入

import random
import string


def RPG():
    while True:
        try:
            RPG = ""
            count = 0
            length = int(
                input("How many characters would you like in your password? "))
            break
        except (ValueError, UnboundLocalError):
            print("Please enter a number.")
    while count != length:
        upper = [random.choice(string.ascii_uppercase)]
        lower = [random.choice(string.ascii_lowercase)]
        num = [random.choice(string.digits)]
        symbol = [random.choice(string.punctuation)]
        everything = upper + lower + num + symbol
        RPG += random.choice(everything)
        count += 1
        continue
    if count == length:
        print(RPG)
# could also use string.printable for digits, letters, punct., and whitespace.


RPG()

原始代码的问题在于,
count!=长度
始终得到执行,无论是
尝试除外
部分。如果未引发
ValueError
UnboundLocalError
,则只能继续执行
while
循环来避免此问题。通过在
try except
之前初始化
c=1
,并仅在
try
部分将其更改为
0
,程序仅在未发生异常的情况下进行
循环

import random
import string


def RPG():
    c=0
    try:
        RPG = ""
        count = 0
        length = int(
            input("How many characters would you like in your password? "))
    except (ValueError, UnboundLocalError):
        print("Please enter a number.")
        c=1
    if c==0:
        while count != length:
            upper = [random.choice(string.ascii_uppercase)]
            lower = [random.choice(string.ascii_lowercase)]
            num = [random.choice(string.digits)]
            symbol = [random.choice(string.punctuation)]
            everything = upper + lower + num + symbol
            RPG += random.choice(everything)
            count += 1
            continue
        if count == length:
            print(RPG)
# could also use string.printable for digits, letters, punct., and whitespace.


RPG()

原始代码的问题在于,
count!=长度
始终得到执行,无论是
尝试除外
部分。如果未引发
ValueError
UnboundLocalError
,则只能继续执行
while
循环来避免此问题。通过在
try except
之前初始化
c=1
,并仅在
try
部分将其更改为
0
,程序仅在未发生异常的情况下进行
循环

import random
import string


def RPG():
    c=0
    try:
        RPG = ""
        count = 0
        length = int(
            input("How many characters would you like in your password? "))
    except (ValueError, UnboundLocalError):
        print("Please enter a number.")
        c=1
    if c==0:
        while count != length:
            upper = [random.choice(string.ascii_uppercase)]
            lower = [random.choice(string.ascii_lowercase)]
            num = [random.choice(string.digits)]
            symbol = [random.choice(string.punctuation)]
            everything = upper + lower + num + symbol
            RPG += random.choice(everything)
            count += 1
            continue
        if count == length:
            print(RPG)
# could also use string.printable for digits, letters, punct., and whitespace.


RPG()

如果代码的第一部分处理和异常,那么
length
永远不会被分配。那么我该怎么办?如果代码的第一部分处理和异常,那么
length
永远不会被分配。那么我该怎么办?我已经测试过了。当您输入一串字母时,它会打印“请输入一个数字”,并重复输入请求。你确定你运行了我的代码吗?你是对的,我尝试粘贴时出错了;不幸的是,我不能对你的答案投赞成票或反对票,因为我几乎没有声誉,但我希望人们看到这条评论,知道你的代码实际上是正确的。这也是一个循环,如果用户输入字符串,那么奖金!谢谢你的帮助,我已经测试过了。当您输入一串字母时,它会打印“请输入一个数字”,并重复输入请求。你确定你运行了我的代码吗?你是对的,我尝试粘贴时出错了;不幸的是,我不能对你的答案投赞成票或反对票,因为我几乎没有声誉,但我希望人们看到这条评论,知道你的代码实际上是正确的。这也是一个循环,如果用户输入字符串,那么奖金!谢谢你的帮助。