Quiz Program – Part 2: adding to the quiz

Option 1.2 – Presenting the final score

Now lets look at how we can give a personalised message back to the user.

Code helper image

So, if they get 5 out of 5 we can tell them that they aced it. But if they get 1/5 we can say something else.

This is pretty easy to do at a basic level.

  1. Add this code at the bottom of your program – underneath where you gave them the score
  2. Use a series of  if  lines to give the detailed feedback – don't forget to use == in the formula or to add the : at the end
  3. Don't forget that their score could be 0
  4. Run your code to check that it works

That's the basic level.

A better method

A better way to do this is to use a command called  elif  which stands for else if and can make your code easier to write.

This also makes it easier to deal with odd scoring systems – say if you have negative scores or really high scores possible.

Code helper image
  1. Start with an if line as before and deal with the top mark possible
  2. Then use an  elif  line to deal with the next highest set of score. In this case I'm using  elif score >= 3:  (if the score is greater than or equal to 3)

    This will deal with scores of 3 or 4 – but not 5 as that's already been dealt with

  3. The use  elif score >= 1:  to deal with scores of 1 or 2
  4. And then use a simple  else:  line to deal with any other score (in this case, 0 or below)

    Don't forget the : (the colon) at the end of each if, elif or else

This is all much quicker than dealing with each of the scores one by one.

It works because Python will only ever pick one of the options in a set of if, elif and else commands.

So, if the score is 4, the second option gets picked. At that point, Python ignores everything else in the stack of ifs – so it never even looks at the >= 1 option.