Scientific Computing with Python: More Conditional Structures

I chose line 3 because if it does blow up or not, line 4 will get executed since fahr is a floating point number and the calculation in line 4 can be done. I don’t know why the correct answer is lines 3&4.

Thanks,

1 Like

If line three fails, then there is no fahr variable defined for line four

1 Like

Well, it depends on what you put under “except”. If you create the variable fahr within the “except”, when computing the calculation, fahr is defined. But of course, that would give a incorrect value of cel.

An alternative way would be to also include also the print(cel) statement so we only execute it only if it runs smoothly. In the case there is an exception, we only display a friendly error message.
Note: Experts, please confirm that my suggestion is a good practice. Thank you!

Blockquote
temp = “5 degrees”
cel = 0
try:
fahr = float(temp)
cel = (fahr - 32.0) * 5.0 / 9.0
print(cel)
except:
print (“There is an error. Please enter a number next time”)

Blockquote

1 Like

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.