简体   繁体   English

Unbound Local Error:在try和except语句内进行赋值之前引用的局部变量

[英]Unbound Local Error: local variable referenced before assignment inside of a try and except statement

I am attempting to create a very simple menu system, using all of the pythonic tools, (try and except statements, loops, if statements) and I have run into a little bit of trouble. 我试图使用所有pythonic工具(尝试和除语句,循环,if语句)创建一个非常简单的菜单系统,但遇到了一些麻烦。

This is the code and error message i have at the moment 这是我目前的代码和错误消息

def Menu():
while True:
    print("""
    Hello, please enter a number 1 - 4
        1 - Compliment
        2 - Fact
        3 - Insult
        4 - Quit
        """)

    try:
        UserInput_INT = int(input("> "))


    except ValueError:
        UserInput_STR = (UserInput_INT)
        if len(UserInput_STR) == 0:
            print("You have entered nothing. Please enter a number between 1 and 4")
        print("You entered a character. Please enter a number between 1 and 4")
        Menu()

    if  UserInput_INT not in range (1, 5):
        print("Invalid input please enter a whole number between 1 and 4")
        continue

    UserInput_STR = (str(UserInput_INT))
    if UserInput_STR == '1':
        print(" You look horrible today!")

    elif UserInput_STR == '2':
        print("Sam Birkenshaw & Jordan Ives can code better than Mr Bath. ")

    elif UserInput_STR == '3':
        print("You are bad at coding ")

    elif UserInput_STR == '4':
        quit()

Menu() 菜单()

error message: 错误信息:

"Traceback (most recent call last): File "E:/All/School Work/Computer Science/Code/SAM broken code.py", line 12, in Menu UserInput_INT = int(input("> ")) ValueError: invalid literal for int() with base 10: '' “跟踪(最近一次通话最近):文件“ E:/ All /学校/计算机科学/代码/ SAM损坏的code.py”,菜单UserInput_INT = int(input(“>”)))第12行,ValueError:无效以10为底的int()的文字:''

During handling of the above exception, another exception occurred: 在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last): File "E:/All/School Work/Computer Science/Code/SAM broken code.py", line 39, in Menu() File "E:/All/School Work/Computer Science/Code/SAM broken code.py", line 16, in Menu UserInput_STR = (UserInput_INT) UnboundLocalError: local variable 'UserInput_INT' referenced before assignment" 追溯(最近一次通话):文件“ E:/ All /学校/计算机科学/代码/ SAM损坏的code.py”,第39行,位于Menu()文件“ E:/ All /学校/计算机科学/ “代码/ SAM损坏的code.py”,菜单UserInput_STR =(UserInput_INT)中的第16行,UnboundLocalError:分配前引用了本地变量“ UserInput_INT”

I need to have it so that if the user enters nothing, there is a different message displayed than if they enter a letter, and if they enter something other than one of the accepted answers. 我需要它,以便如果用户不输入任何内容,则显示的消息不同于他们输入字母以及输入的字符不是接受的答案之一。

(I am runing python 3.6.2 currently) (我目前正在运行python 3.6.2)

The value that is being passed as input to the program is not a valid integer. 作为输入传递给程序的值不是有效的整数。 Now, when an exception is raised the variable UserInput_INT is not being assigned causing the second error. 现在,当引发异常时,未分配变量UserInput_INT导致第二个错误。 Try checking if the value is integer before trying to cast it. 尝试在转换前将值检查是否为整数。

input = input("> ")
UserInput_INT = int(input) if input.isdigit() else input

As a side note, please try to follow naming conventions. 作为旁注,请尝试遵循命名约定。

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

相关问题 未绑定错误,分配前已引用局部变量 - Unbound error, local variable referenced before assignment 我的错误处理代码尝试/预期代码给我错误“在使用 try 和 except 语句赋值之前引用的局部变量” - My error handling code try/expect code giving me error "local variable referenced before assignment with try and except statement" 全局变量声明+未绑定错误:分配前已引用局部变量? - Global variable declaration + Unbound Error: local variable referenced before assignment? 赋值前引用的未绑定局部变量 - Unbound local variable referenced before assignment Python 3 UnboundLocalError:在try语句中赋值之前引用的局部变量 - Python 3 UnboundLocalError: local variable referenced before assignment in try statement 尝试除值错误,未绑定局部变量 - try and except value error , unbound local variable try and if - 赋值前引用的局部变量 - try and if - local variable referenced before assignment 获取未绑定的本地错误:分配下面引用的局部变量“x” - Getting Unbound local Error: local variable 'x' referenced below assignment Python:得到了“分配前引用的局部变量‘idx’”的错误语句 - Python: Got an Error Statement for "local variable 'idx' referenced before assignment" 赋值之前引用的局部变量“ statement” - local variable 'statement' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM