Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
This program should give A for 90 and more, B for 75 to 89, C for 50 to 74, and D for the rest. But for the marks 80 it shows the wrong grade.
Say what it shows, why, and write the program in the right order.
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. Follow the conditions from the top, for marks = 80.
marks >= 50: is 80 at least 50? True. The grade is C, and Python skips all the other parts.So the program shows C. The expected grade is B. The conditions marks >= 75 and marks >= 90 are never asked, because the first one catches every mark of 50 and more.
The fix. Start with the highest step, so that each check only needs to ask about its own step.
Check. For 80: 80 >= 90 is False, 80 >= 75 is True, so B. For 49, all three are False and the else gives D.
Answer: The program shows C. The most special condition must come first.
Log in to join the discussion.
Loading…