python - Python3 except ValueError: if/else -
i want make try/except ensure inputted value float variable. want make exception if inputted value 'g' want redefine variable '9.8' instead of telling me must number. below try there seems problem it. ps ignore mistake of printing "must integer"
your example won't work because if valueerror occured, variable a doesn't exist. python interpreter tried create variable a not done raised error. therefore cannot use a inside except block.
you should first save input("a:") variable , test if it's value "g". if is, set "9.8". next step transform variable float.
all should done inside try block.

Comments
Post a Comment