Python3执行分配错误
我有一段Python 3代码,它给我带来了一些问题:Python3执行分配错误,python,python-3.x,Python,Python 3.x,我有一段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 rig
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()
这将导致提示
只需在…中键入一个数字。
…确实如此。如果我输入的不是浮点,它会给出错误
错误!再试一次代码>
…并重新提示我。到目前为止,一切顺利。然而,如果我输入一个正常的数字,它会显示
无错误
…当它应该显示变量customErrorMessage
时,在本例中为
-!-错误消息--代码>
我最初认为问题在于在exec()
函数中,err
没有被视为全局变量,而是使用了global err;err=1
而不仅仅是err=1
无法修复它。直截了当地说:
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()
您的错误值从未更改。它始终为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()
问题原因:
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()
1.exec是python3中的一个函数,因此,如果在其中执行任何变量赋值,它将不会更改变量内容,它将仅对exec函数可用
即)
请提供一个简单的例子。特别是,输入或其求值或循环都不是必需的。另请参见。这是精简版。。。它显示了我在消除不必要代码的同时所做的基本想法。代码的问题是,当您测试输入variable=inputType(input(text))
时,实际上是将您输入的内容转换为inputType。因此,当您键入一个整数时,它会将数字转换为浮点型。@Digvijayad这是如何导致问题的?您希望在用户输入整数时出错,但您的输入命令会将输入转换为浮点型,因此不会出错。尝试在此之后打印变量<代码>变量=输入类型(输入(文本))
打印(变量)
。你会看到我的意思我以为我在我的“我最初的想法”部分中解决了这个问题?@Quelklef你看到链接了吗?你看到我的部分了吗?它应该是有效的,这一事实也证明了这一点:我有点像Python noob,所以我在理解你的链接时遇到了一些困难。@Quelklef Research会在一会儿左右说