- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试将代码“排除”为 int 的字符串。 - “长度”。当我输入 except ValueError 时,我返回“请输入数字”,但添加了更多错误。我还添加了 except 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()
这是我使用此代码并在 length 中输入字符串而不是整数得到的结果:
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
我只期望“请输入数字”,而不是其余的错误,任何帮助将不胜感激。感谢您的宝贵时间!
最佳答案
原始代码的问题是,尽管 count != length
总是被执行,无论 try- except
部分如何。如果未引发 ValueError
或 UnboundLocalError
,则可以通过仅继续进行 while
循环来避免这种情况。通过在 try-except
之前初始化 c=1
并仅在 try
部分将其更改为 0
,如果没有发生异常,程序只会进入 while
循环。
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()
关于python - 无法弄清楚为什么我收到 UnboundLocalError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54540545/
以下代码在 Python 2.5 和 3.0 中都按预期工作: a, b, c = (1, 2, 3) print(a, b, c) def test(): print(a) prin
以下代码在 Python 2.5 和 3.0 中都按预期工作: a, b, c = (1, 2, 3) print(a, b, c) def test(): print(a) prin
从 string 导入时模块,与解析函数一起使用。 from string import punctuation def parsing_func(data): if not any(i==v
mt=0 ft=0 def amount_total(g): if g=="male": mt=mt+amount else: ft=ft+amount
我正在尝试在 leetcode 中解决这个问题。我是 leetcode 的新手。我收到 UnboundLocalError: local variable 'i' referenced before
这个问题已经有答案了: UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned
也许我今天早上的咖啡不够浓,但这种行为现在让我感到困惑: >>> a = 'foo' >>> def func1(): ... print a ... >>> def func2(): ...
我是编程新手,但我喜欢自然语言(如您所见)。我正在尝试编写一个简单的程序来帮助测试法语现在时动词。但是,在这段代码中,我不断收到错误消息,因为 CorrectAnswers 显然尚未设置为全局变量。有
def create_school(school,year): all_students = {} achievements= {} var = data.Files.get(
我正在使用 django 1.8 和 python 2.7,但我不断收到 unbounLocalError。我的views.py 中的代码看起来一切正常。但它保留分配之前引用的局部变量“cd”。如果有
例如: $ cat -n foo.py 1 def f(): 2 str = len 3 str = str('abc') 4 #
def main(): cash = float(input("How much money: ")) coins = 0 def changeCounter(n): whil
我对局部变量和 python (2.7) 有一些小问题。 我有一些代码: def foo(a): def bar(): print a return bar() >>>
这个问题在这里已经有了答案: Using global variables in a function (25 个答案) 关闭 4 年前。 所以,我一直在使用 python 作为命令提示符应用程序开
我刚开始编程并尝试写一些东西但(当然)它失败了。在我找到真正的问题之后:UnboundLocalError。因此,为了让您远离周围的所有废墟,我将代码简化为: def test(): try:
我继承了一个遗留单元测试,它试图覆盖嵌套在另一个类中的自定义类的名称 datetime。我现在正在重构它(我同意这很糟糕)但我不明白我看到的特定错误。 我看到了一个UnboundLocalError,
这个问题在这里已经有了答案: UnboundLocalError trying to use a variable (supposed to be global) that is (re)assig
这个问题在这里已经有了答案: UnboundLocalError on nested module reimport [duplicate] (2 个答案) 关闭 4 年前。 我被 python 解
这个问题在这里已经有了答案: Is it possible to modify a variable in python that is in an outer (enclosing), but n
我在 View 中有一个函数,用于上传 CSV 数据并写入数据库。那部分正在工作。但是,当我尝试检查是否有数据更新或创建时,我收到一条错误消息: "local variable 'created' r
我是一名优秀的程序员,十分优秀!