I have this Python 3 code which is causing me issues:
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()
This should cause the prompt...
Just type a number in...
...And it does. And if I type in something other than a float, it gives the error...
Error! Try again!
...And re-prompts me. So far, so good. However, if I put in a normal number, it displays...
No Error
...When it should display variable customErrorMessage
, in this case being...
-!- error message -!-
I originally thought that the issue was that in the exec()
function, err
wasn't being treated as a global variable, but using global err; err = 1
instead of just err = 1
doesn't fix it.