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"

enter image description here

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

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -