246. Python Program to Make a Quiz Game That Gives a Score
MediumPhase 2 · Practice
This one takes a few steps. It uses only a list, a for loop and the string methods strip() and lower(), and you know them all.
Overview
Quiz apps check each answer and keep a score. School tests and game shows use the same idea.
Problem Statement
Mei makes a 5-question quiz for a class. These are the questions and their right answers:
- What is the capital of Japan? Answer:
tokyo - How many days are in a week? Answer:
7 - What color do you get when you mix blue and yellow? Answer:
green - What is 12 times 12? Answer:
144 - Which planet is closest to the Sun? Answer:
mercury
Store these 5 answers in your program. Then read the player's 5 answers, one per line, in question order.
To mark an answer:
- Remove the spaces at its start and at its end. Spaces inside the answer stay.
- Make every letter small.
- The answer is right only when the result is exactly the stored answer.
So Tokyo and TOKYO are right. Tokyo City and seven are wrong: the program compares text, not meaning.
For each question, print one line:
Q<number>: rightwhen the answer is right.Q<number>: wrong, the answer is <answer>when it is wrong, with the stored answer.
Last, print Score: <score>/5, where the score is the number of right answers.
Examples
Example 1. Input:
Tokyo
7
blue
144
MERCURY
gives:
Q1: right
Q2: right
Q3: wrong, the answer is green
Q4: right
Q5: right
Score: 4/5
Why: Tokyo becomes tokyo, and MERCURY becomes mercury once the spaces and capitals are gone. Only blue is wrong, so the score is 4.
Example 2. Input:
kyoto
seven
green
124
mars
gives:
Q1: wrong, the answer is tokyo
Q2: wrong, the answer is 7
Q3: right
Q4: wrong, the answer is 144
Q5: wrong, the answer is mercury
Score: 1/5
Why: only green matches. seven means 7, but the text is not 7, so it is wrong.
Input
Five lines: the player's answers to questions 1 to 5, in order. Each answer has at least one letter or digit. It may have capital letters, and spaces at its start or end. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
Six lines: one for each question, then the score. The tests try 7 different inputs, also all right, all wrong, and answers with spaces at the start and end.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.