Python for Everybody - More Conditional Structures

Tell us what’s happening:

RE: the question below the video.

I think the answer is 3, 4, 5 not 3,4.

If you only surround 3 and 4 by a try block, it will perform ‘except:’ and also perform ‘print(cel)’.

Since cell = 0, it will print 0, which I don’t think you want it to do.

If you surround 3,4 and 5 with a try block, it will perform ‘except’ and not ‘print (cel)’.

Your code so far

temp = "5 degrees"
cel = 0
try:
    fahr = float(temp)
    cel = (fahr - 32.0) * 5.0 / 9.0
    print(cel)
except:
    print('Not a number')

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36

Challenge Information:

Python for Everybody - More Conditional Structures

Why not?

The operation is valid no matter what happens on lines 3 and 4. Only lines 3 and 4 can fail.

Just for context, I’ve only started learning Python in the last day, and I’ve had almost no experience coding before then, so I know almost none of the vocabulary.

Which what do you mean by operation? Is the operation the whole code? or a specific part of the code?
What is a valid operation? Is it an operation that doesn’t return an error?

If by valid you mean ‘doesn’t cause an error’, that makes sense - I can see why only 3 and 4 need to be surrounded by a try block for the code not to return an error.

Are you saying that the question is only asking me how to avoid an error?

If I want the code to only print correct values for cel, it seems like I should put print(cell) in the try block.
By correct values, I mean values that are the Celsius equivalent of the Fahrenheit input.

Essentially, yes. That’s what the question is asking - what’s the minimum number of lines that absolutely must be guarded by a try.

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