──────────────────────────────────────

38. Pocket Quiz Capstone · Get One Round Working

Make one complete round before extending the quiz to several questions.

Lesson files · lessons/foundations-capstone/build-a-round

Before you start
- Define a function that takes arguments and returns a value.
- Use strip() and lower() to clean a reply.
- Use if to choose an output from a comparison.

By the end
- Use a function to compare a cleaned reply with a supplied key.
- Use the returned True or False to score one reply.
- Test one correct reply and one reply that should score zero.

Where you can use it
Build one question for a spelling game. Test the reply checker before adding more questions.

Test one answer before you build the full quiz. In is_correct(reply, expected), clean the reply and compare it with a tidied, lowercase answer key. The comparison returns True or False.

Use an if in the code that calls the function to display a point for a match. Try the same answer with extra spaces and a capital letter. When the function works, you can reuse it with every question in the full quiz.

Predict
Why pass expected as a parameter?
- It forces every answer to be mars.
- The same comparison can serve different questions.

Worked example (example.py)

def is_correct(reply, expected):
    return reply.strip().lower() == expected

print(is_correct(" MARS ", "mars"))


Example result
True

Read an answer, compare it to mars using is_correct, and print 1 for correct and 0 for wrong.

Starter program (starter.py)

def is_correct(reply, expected):
    return reply.strip().lower() == expected

print(is_correct(" MARS ", "mars"))


Hint
Use if is_correct(input(), "mars") to choose 1 or 0.

Case 1. Try the sample
Input
 Mars 
Expected output
1

Case 2. Wrong answer
Input
venus
Expected output
0

Case 3. Blank answer
Input
   
Expected output
0

Stretch
Test is_correct with a second answer key.

Reflect
What did you learn by building one round before building all three?

Check your prediction
The same comparison can serve different questions. A parameter lets each call supply a different answer key.
