Skip to main content
 首页 » 编程设计

Python 3 执行分配错误

2025年05月04日9虾米哥

我有导致我出现问题的 Python 3 代码:

def setStartingVariable(inputType, text, code = None, customErrorMessage = "Error: No error message"): 
    while True: 
        try: 
            variable = inputType(input(text)) # Test if the inputted variable is of the right type. Keep in mind inputType is a variable, not a function. 
        except BaseException:  
            print("Error! Try again!") 
            continue 
        err = 0 # Reset 
        if code != None: 
            exec(code) 
        if err == 0: 
            print("No error") 
            return variable 
        elif err == 1: 
            print(customErrorMessage) 
        else: 
            print("Error: var err != 1 and != 0") 
 
 
def inputStartingVariables(): 
    global wallet 
    wallet = setStartingVariable(float, "Just type a number in... ", "err = 1", "-!- error message -!-") 
 
inputStartingVariables() 

这应该会导致提示...

只需在...中输入一个数字

...确实如此。如果我输入的不是 float ,它会给出错误...

错误!再试一次!

...并重新提示我。到目前为止,一切都很好。但是,如果我输入一个普通数字,它会显示...

没有错误

...当它应该显示变量 customErrorMessage 时,在这种情况下是...

-!- 错误信息-!-

我最初认为问题是在 exec() 函数中,err 没有被视为全局变量,但使用 global 错误; err = 1 而不是 err = 1 并不能解决问题。

请您参考如下方法:

直截了当:

您的错误值永远不会改变。它始终为 0

使用 exec() 不会改变它

此更改将回答您的问题:

def setStartingVariable(inputType, text, code = None, customErrorMessage = "Error: No error message"): 
    while True: 
        try: 
            variable = inputType(input(text)) # Test if the inputted variable is of the right type. Keep in mind inputType is a variable, not a function. 
        except BaseException:  
            print("Error! Try again!") 
            continue 
        exec_scope = {"err" :0} 
         # Reset 
        if code != None: 
            print ("here") 
            exec(code,exec_scope) 
 
        print (code) 
        if exec_scope["err"] == 0: 
            print("No error") 
            return variable 
        elif exec_scope["err"] == 1: 
            print(customErrorMessage) 
        else: 
            print("Error: var err != 1 and != 0") 
 
 
def inputStartingVariables(): 
    global wallet 
    wallet = setStartingVariable(float, "Just type a number in... ", "err = 1", "-!- error message -!-") 
inputStartingVariables() 

问题原因:

1.exec 是 python3 中的一个函数,所以如果你在里面做任何变量赋值,它不会改变变量内容,它只对 exec 函数可用

即)

def a(): 
    exec("s=1") 
    print (s) 
a() 
 
Traceback (most recent call last): 
File "python", line 4, in <module> 
File "python", line 3, in a 
NameError: name 's' is not defined 

有关变量赋值的更多信息,您可以通过 martin 查看这两个问题和 blaknight

编辑:

def setStartingVariable(inputType, text, code = None, customErrorMessage = "Error: No error message"): 
    global err 
    while True: 
        try: 
            variable = inputType(input(text)) # Test if the inputted variable is of the right type. Keep in mind inputType is a variable, not a function. 
        except BaseException:  
            print("Error! Try again!") 
            continue 
 
        err = 0 # Reset 
        if code != None: 
            exec("global err;"+code) 
            print (err) 
        if err == 0: 
            print("No error") 
            return variable 
        elif err == 1: 
            print(customErrorMessage) 
        else: 
            print("Error: var err != 1 and != 0") 
 
 
def inputStartingVariables(): 
    global wallet 
    wallet = setStartingVariable(float, "Just type a number in... ", "err = 1", "-!- error message -!-") 
 
inputStartingVariables()