Don't understand try/except structure

temp = "5 degrees"
cel = 0
fahr = float(temp)
cel = (fahr - 32.0) * 5.0 / 9.0
print(cel)

I am not understanding. At times cel=0 and at times cel=(something else). Why and How?
Related to Py4e More Conditional Structures
Please reply a copy to mimijsr91@gmail.com

That isn’t going to fly…
float of temp (an alpha numeric string) isn’t going to work.

The “try” bock is something the interpreter TRIES to do. If it cannot be done, the “except” block is executed instead.

So you gotta think what might be impossible for the interpreter to understand and do.
Can it divide by zero? No, so any division that could end up dividing by zero must be wrapped in a try-except.
Can it turn “Hello I’m John” cast to in integer? No, because that’s in no way a valid number-format and so it has no idea what you want → again needs a try-except block.
Every time the program runs a line of code that could run into an error, but you don’t want it to terminate, you need a try-except block. Obviously oftentimes programs are written to catch invalid inputs beforehand or have other logical structures to avoid errors.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.