Python for Everybody -- why is line 4 included in the try block?

Hi guys,

I’ve been going through the P4E lecture series & just finished the segment for conditional structures, in particular the part that introduces the statements try and except.

What I don’t understand in this lecture is the answer for the mini-quiz tied to the lecture, which according to the website consists of lines 3 and 4. The reasoning for line 3 was pretty self-explanatory for me, but I really can’t wrap my head around why line 4 is included since the odds of it returning a traceback error depend on the outcome from line 3. Like, wouldn’t it be unnecessary to include line 4 in the try block if I include after except: functions (re:findall?) that extract the number in temp and convert it into a float/integer for fahr?

I’ve attached the screenshot of the problem below; somebody please help me out here!

i asked the exact same thing for this and I came up with my own explanation. Let me look it up for you on here…

edit:
here it is

1 Like

Hey, many thanks!

The question definitely does make a lot more sense if I substitute the arbitrary input that is “5 degrees” into an unknown, undesignated hypothetical variable that may or may not cause an error…so the whole point is to view each of these lines separately & think in correlation with the variable to consider whether they would cause any issue. I hope I got the rationale right!

well the 2 lines (3,4) work together here because they are meant to convert a number to a float then use that float in a calculation. So you would likely need to handle both of these at the same time.
People use try/except for various reasons. Sometimes it is just to catch errors in a ‘nice way, and allow a graceful exit’ in which case you want both of these inside the try block.
Sometimes it is to fix things in-spite of the failure. (in which case maybe only line 3 would need it if it handles the change to a proper value somehow)

1 Like

Lines 3 and 4 belong in a try block together because 3 creates a variable that will be used in 4. If line 3 fails, fahr won’t exist and line 4 will raise a NameError. There are alternatives. You could use two try blocks, but what does that get you? You could put only line 3 in a try block and define fahr in the except: block, but what should it be? You already have a default value for cel. You could also conclude that the correct behavior for an unknown input is undefined for temperature conversion, not use any try blocks, and allow the ValueError to be raised. I would consider that to be the most correct thing but this is just a teaching example.

1 Like

Thanks a lot!

I see, so the point is that the example given in the question doesn’t really resonate with what would be the best practice in the real world, right? And that despite such, for the purpose of this lecture both lines 3 and 4 should be considered in conjunction for a try block since they deal with the same variable “fahr” and hence are grouped together in terms of their function. Am I getting this correctly?

Ah great! So ultimately the reason lines 3 & 4 are grouped together here comes down to the fact that their operation both deals with the variable “fahr” & are kinda correlated to each other for this reason. Am I getting it right this time?

That is what I think, yes.

1 Like

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