Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
For each job, say whether you would look before you leap (an if) or try and catch (try and except), and why.
no mark if there is none.Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. If one short if makes the check, look first. If the check is as hard as the job, try and catch.
if name in marks is a short check. marks.get(name, "no mark") is shorter still. Look before you leap fits well.int() already knows, so use try with except ValueError.if items: is a short check: an empty list is False. Then use items[0].if divisor != 0: is short and clear. (Try and catch with ZeroDivisionError also works.)Check. Ask: is the check shorter and easier than the job? If yes, check first.
Answer: look (or get), try, look, look.
Log in to join the discussion.
Loading…