Build a Python Indentation Quiz
Build a terminal quiz while learning Python indentation, loops, and functions.
Introduction
30 Second Summary
A few spaces can decide which instructions a program groups together. When those spaces drift out of place, a tiny layout mistake can stop the whole program.
In this project, you will build a three-question terminal quiz with Python. You will trigger a real IndentationError before repairing the blocks that control questions and scoring.
What You'll Build
You will launch your quiz in Command Prompt, answer three questions, receive immediate feedback after each response, and finish with a score you can show from the terminal.
By the end of this project, you'll have:
- A quiz with cleaned answer input that handles surrounding spaces and capitalization before checking each response.
- A reusable question loop that asks all three prompts and shows the matching correct or incorrect feedback.
- A quiz you can deliberately break, diagnose from its traceback, repair, and rerun to see one accurate final score.
- Secret Mission: Add nested score feedback that changes for perfect, near-perfect, and lower scores.
Are there any prerequisites?
A few basic Python exercises are helpful. You need a Windows computer with Python installed, plus Notepad and Command Prompt.
Before We Start
This short checkpoint locks in the quiz you are about to build. You will use four-space indentation to control function, loop, and conditional blocks before reading and repairing an intentional IndentationError.
Set Up Your Windows Workspace
Your quiz needs a reliable launch point before indentation becomes the focus. A confirmed Python command keeps setup problems separate from the block errors you will study.
You will prepare Notepad as your editor. You will run myscript.py from Command Prompt to prove that your computer can execute the saved script.
In this step, get ready to:
- Confirm Python 3 from Command Prompt.
- Save the readiness script as myscript.py in Notepad.
- Run the saved script from its folder.
Verify Python in Command Prompt
Command Prompt sends commands directly to programs installed on your computer. The first check confirms that the python command reaches a Python 3 interpreter.
- Press the Windows key to open Start search.
- Type Command Prompt into the search box.
- Select Command Prompt from the results.
Before the check, pause over whether you expect the result to begin with Python 3.
- Check the installed Python version by running this command:
python -V
What Does This Command Do?
The python command starts the installed interpreter. The -V option prints its version before exiting.
✔️ I see Python 3
That clears the first setup hurdle. Your python command is ready to run the quiz scripts in this project.
ⓧ I see an older version
This project requires Python 3. Update the interpreter before you continue.
- Visit the official Python downloads for Windows page.
- Download the current Python 3 installer.
- Follow the installer prompts to complete the update.
- Return to Command Prompt from earlier.
- Repeat the version check above.
- Confirm that the output begins with Python 3.
ⓧ Command not found
Command Prompt cannot find a Python installation through the python command. Install Python 3 before creating the script.
- Visit the official Python downloads for Windows page.
- Download the current Python 3 installer.
- Follow the installer prompts to complete the installation.
- Return to Command Prompt from earlier.
- Repeat the version check above.
- Confirm that the output begins with Python 3.
Python Check Still Failing?
Close Command Prompt after installing Python. Open it again through Start search so the new window can detect the installation.
If the check still fails, help me troubleshoot the Python command.
Create the readiness script in Notepad
A saved script keeps every line in one place each time you run it. This persistence becomes important when you inspect indentation levels later.
- Press the Windows key to open Start search.
- Type Notepad into the search box.
- Select Notepad from the results.
- Add the readiness message by entering this code:
print("Indentation Rescue is ready.")
What Does This Code Do?
The print() function writes the readiness message to the terminal. Seeing that message proves Python executed the line in your saved file.
- Click File in the Notepad menu.
- Select Save As.
- Choose an existing folder that you can find again.
- Record the full path shown for that folder here: full path to the folder containing myscript.py.
- Enter myscript.py in the File name box.
- Select Save.
- Confirm that the Notepad title shows myscript.py.
Saved With the Wrong Extension?
Use Save As again if the visible filename has an extra text extension. Make sure the final filename ends with .py.
If the filename still looks wrong, help me save a Python file in Notepad.
✔️ Awesome, I've got everything!
Your myscript.py file is saved with the readiness message.
ⓧ I'd like to double check the full code
Your complete myscript.py file should match this code:
print("Indentation Rescue is ready.")
Run the script from its folder
Command Prompt runs files relative to its current directory. Moving to the folder that contains myscript.py gives Python a direct path to the script.
- Return to Command Prompt from earlier.
- Move Command Prompt to the folder containing myscript.py by running this command:
cd /d "[[SCRIPT_FOLDER="full path to the folder containing myscript.py"]]"
What Does This Command Do?
The cd command changes the current directory. The /d option also switches drives when your saved folder uses a different drive.
Before you run the file, pause over the single line you expect the terminal to print.
- Run the saved script by entering this command:
python myscript.py
What Does This Command Do?
The python command opens myscript.py with the verified interpreter. Python executes the file from top to bottom.
You should see Indentation Rescue is ready. below the command.
That's your first working result. Command Prompt can now execute your saved Python file from the correct folder.
Script Not Running?
Check that the Command Prompt line ends with your recorded folder path. Check that Notepad saved the file as myscript.py with no extra extension.
If the command still fails, help me run my saved Python script.
Your Windows workspace is ready. Next, you will turn the readiness line into a question with two visible answer branches.
Build One Working Question
Your saved script already proves that Python can run myscript.py from its folder. Now the file needs to react to what someone types.
A single working question gives you a visible quiz immediately. It keeps the first indentation change small before deeper blocks arrive.
In this step, get ready to:
- Normalize one typed answer with input().
- Build aligned if and else branches with four-space bodies.
- Confirm the feedback from both answer paths.
Collect and clean one answer
Answers typed in a terminal can contain capital letters or surrounding spaces. Cleaning the response gives your quiz one consistent value to compare.
- Switch back to myscript.py in Notepad.
- Replace print("Indentation Rescue is ready.") by pasting the code below:
answer = input("Which keyword starts a condition in Python? ").strip().lower()
What Does This Line Do?
- input() displays the question.
- input() waits for a typed response.
- answer stores the returned text.
- .strip() removes surrounding whitespace.
- .lower() converts cased characters to lowercase.
- Save myscript.py in Notepad.
- Test the saved file from Command Prompt by running this command:
python myscript.py
What Should You See?
The command executes the saved myscript.py file. You'll see the quiz question waiting for an answer.
- Type IF as your answer.
- Press Enter.
You'll see the question accept your answer before Command Prompt returns.
Question Not Appearing?
Confirm that Command Prompt still points to the folder containing myscript.py.
Confirm that the updated file is saved with its .py ending.
Help me troubleshoot why my Python question is not appearing.
Add the conditional branches
The colon after each branch header opens a code block. Four spaces show which print statement belongs to that block.
- Return to myscript.py in Notepad.
- Paste the code below on a new line beneath the answer = input("Which keyword starts a condition in Python? ").strip().lower() line:
if answer == "if":
print("Correct!")
else:
print("Not quite. The answer is if")
How Do These Branches Work?
- if compares the normalized answer with "if".
- The four spaces make print("Correct!") part of the matching branch.
- else lines up with if to define the alternative branch.
- The second indented print() belongs to else.
- Save myscript.py.
- Test the correct-answer branch from Command Prompt by running this command:
python myscript.py
What Does This Run Test?
The command starts the quiz at its question. The branch decision happens after you submit an answer.
- Type if as your answer.
- Press Enter.
You'll see Correct! because the normalized answer matches the condition.
Correct Answer Not Matching?
Check that the condition uses answer == "if" with two equals signs.
Check that print("Correct!") starts with four spaces.
Help me fix my correct-answer branch.
Compare the branch results
Your first run proved that the matching branch works. One different answer lets you test the alternative branch.
Before you run it again, which branch do you expect a different answer to enter?
- Test the other answer path by running the script again:
python myscript.py
What Does This Run Check?
This second run starts with the same normalized input. A different answer makes the comparison select the other block.
- Type for as your answer.
- Press Enter.
You'll see Not quite. The answer is if because the answer does not match the condition.
Incorrect Feedback Not Appearing?
Check that else: lines up with if answer == "if":.
Check that the feedback line starts with four spaces beneath else:.
Help me fix my incorrect-answer branch.
- Return to myscript.py in Notepad.
- Match the indented print("Correct!") line to the if header.
- Match the indented print("Not quite. The answer is if") line to the else header.
How Does Indentation Show Ownership?
Both branch headers begin at the left edge. Each print statement moves four spaces to the right to show which branch owns it.
Your two runs prove that Python executes exactly one of those indented blocks for each answer.
✔️ Awesome, I've got everything!
Your saved file now contains one normalized question with two working feedback branches.
ⓧ I'd like to double check the full code
The complete file contains the input line followed by its aligned conditional branches.
answer = input("Which keyword starts a condition in Python? ").strip().lower()
if answer == "if":
print("Correct!")
else:
print("Not quite. The answer is if")
That is your first live quiz decision working from typed input. Next, you will turn one question into three with a loop.
Break the Loop on Purpose
Your one-question quiz already checks one answer. It gives immediate feedback.
Repeating those branches for three questions would make the script harder to change. A for loop lets one block process every question in sequence.
In this step, get ready to:
- Build a three-question list.
- Add a loop with its first body line aligned to the left edge.
- Use Python's response to locate the loop boundary.
Build the three-question loop
The questions list keeps each prompt beside its expected answer. A loop body contains the statements that repeat for each pair.
- In Notepad, select the existing contents of myscript.py.
- Paste the code below into myscript.py with the final answer line at the left edge:
questions = [
("Which keyword starts a condition in Python? ", "if"),
("How many spaces are recommended per indentation level? ", "4"),
("Which keyword repeats over a sequence? ", "for"),
]
for prompt, correct_answer in questions:
answer = input(prompt).strip().lower()
How is the loop structured?
- The questions list stores three pairs in quiz order.
- Each parenthesized pair is a tuple. It keeps one prompt attached to one correct answer.
- The for header places each question in prompt. It places the expected answer in correct_answer.
- The answer line uses the current prompt to collect cleaned input.
Test the unfinished loop
The current file is a controlled experiment. Python's response exposes how it reads the loop structure.
- Keep the final answer line at the left edge.
- Save myscript.py.
Before you run it, predict whether the script reaches the first question.
- Test the new loop from Command Prompt by running this command:
python myscript.py
Why did Python stop?
You'll see a traceback instead of a question prompt. Its final line names IndentationError.
The colon after for prompt, correct_answer in questions: opens a loop block. The answer line sits at the same indentation level as the header.
Python therefore finds no statement inside the loop block. It stops before collecting any input.
That controlled failure is a win because Python has pointed directly at the missing loop body.
Seeing a different result?
If a question prompt appears, the answer line is probably indented. Return it to the left edge for this experiment.
If Python reports a different parsing problem, compare the list punctuation with the full-code tab below.
Help me reproduce the intended IndentationError.
✔️ Awesome, I've got everything!
Great. Keep myscript.py saved in this intentionally broken state.
ⓧ I'd like to double check the full code
Your myscript.py file should match this code exactly:
questions = [
("Which keyword starts a condition in Python? ", "if"),
("How many spaces are recommended per indentation level? ", "4"),
("Which keyword repeats over a sequence? ", "for"),
]
for prompt, correct_answer in questions:
answer = input(prompt).strip().lower()
Trace the error to its block
A traceback connects an exception to the source location Python could not process. Reading from the bottom gives you the failure category first.
- Start at the bottom of the Command Prompt output.
- Locate IndentationError on the final line.
- Move upward to the line containing answer = input(prompt).strip().lower().
- Compare its left edge with for prompt, correct_answer in questions: in Notepad.
- Identify the colon at the end of the for header as the point that opens the missing block.
You'll see execution stop before any question appears. The traceback identifies IndentationError at the first statement that should belong to the loop.
You have seen Python reject a loop with no indented body. Next, you'll repair the block structure and turn all three answers into a score.
Repair the Blocks and Add Scoring
The traceback from your last run exposed the missing block. Python cannot run a for statement until at least one indented line belongs to it.
Now you will repair the loop so it owns the prompt plus both feedback branches. You will also add a score that changes inside the correct-answer branch.
In this step, get ready to:
- Repair the indentation inside the question loop.
- Increase a score inside the correct-answer branch.
- Confirm the final score changes between quiz runs.
Repair the loop body
Indentation shows which statements belong to the loop. The conditional sits inside that loop because every answer needs its own feedback.
- In Notepad, find the for prompt, correct_answer in questions: line in myscript.py.
- Select that line through the unindented answer = input(prompt).strip().lower() line below it.
- Replace the selected lines with the repaired block below:
for prompt, correct_answer in questions:
answer = input(prompt).strip().lower()
if answer == correct_answer:
print("Correct!")
else:
print("Not quite. The answer is", correct_answer)
How do these blocks fit together?
- The four-space indentation places answer inside the loop. This makes the prompt run once for each question.
- The if and else lines align because they are two branches of the same condition.
- Each print() line sits one level deeper because it belongs to its branch.
- Save myscript.py.
Before you run the repaired quiz, predict whether it will reach all three prompts.
- Test the repaired loop from the same Command Prompt folder by running this command:
python myscript.py
What does this command do?
This command runs the saved myscript.py file. The loop pauses at each prompt so you can enter an answer.
- Enter one answer after each prompt.
You will reach all three questions. Each answer receives either the correct or incorrect feedback.
That is the indentation error repaired. Your loop can now process every item in the question list.
Still seeing an indentation problem?
Check that answer, if, and else each begin with four spaces. Check that both feedback lines begin with eight spaces.
Use spaces consistently across the block. Mixed tabs can make visually aligned lines behave differently.
Help me compare the indentation levels in my quiz loop.
Add the score and final result
The repaired loop now gives feedback for every answer. A score variable records how many answers reach the correct branch.
The final score belongs after the loop. Dedenting that line to the left edge makes it run once after all three questions.
- In myscript.py, select the block starting at for prompt, correct_answer in questions: through the final incorrect-answer print() line.
- Replace the selected block with the scored version below:
score = 0
for prompt, correct_answer in questions:
answer = input(prompt).strip().lower()
if answer == correct_answer:
print("Correct!")
score += 1
else:
print("Not quite. The answer is", correct_answer)
print("Final score:", score, "out of", len(questions))
How does scoring follow the blocks?
- The score variable starts before the loop so every question contributes to the same total.
- The score += 1 line sits inside the correct-answer branch. Incorrect answers skip it.
- The final print() line returns to the left edge. This keeps it outside the repeated loop body.
- The len(questions) expression supplies the total number of questions.
- Save myscript.py.
Before you run the scored quiz, predict where the final result will appear relative to the three feedback messages.
- Run the scored quiz by using this command:
python myscript.py
What happens during this run?
The loop asks all three questions before execution reaches the dedented final line. Your result appears after the third feedback message.
- Answer all three questions in the terminal.
You will see feedback after each answer. You will then see Final score: followed by your score out of 3.
Score missing or changing incorrectly?
Check that score = 0 aligns with the for line. Check that score += 1 aligns with the correct-answer print() line.
Make sure the final score line returns to the left edge. Extra indentation would place it inside the loop.
Help me trace why my Python quiz score is wrong.
Test changing scores
A reliable score responds to different answer sets. The final output should still appear once because it sits outside the loop.
Before the first test, predict the result for three correct answers.
- Launch a perfect-score test by running this command:
python myscript.py
What does this test prove?
This run checks whether every correct answer reaches the increment line. Three correct answers should produce three increments.
- Enter if for the first prompt.
- Enter 4 for the second prompt.
- Enter for for the third prompt.
You will see three correct feedback messages. The run ends with Final score: 3 out of 3.
Before the second test, predict how changing two answers will affect the score.
- Launch a mixed-answer test by running the same command:
python myscript.py
Why does the score start over?
Each launch runs score = 0 before the loop begins. The new attempt gets its own total.
- Enter if for the first prompt.
- Enter five for the second prompt.
- Enter while for the third prompt.
You will see one correct feedback message plus two incorrect feedback messages. The run ends with Final score: 1 out of 3.
The final score appears once after the third answer. That placement confirms the output belongs outside the loop.
Final score printing three times?
Move the final print() line back to the left edge. A four-space indent makes the line part of the loop.
Help me place the final score outside my Python loop.
✔️ Awesome, I've got everything!
Great work. Double check that you saved myscript.py after repairing the loop and adding scoring.
ⓧ I'd like to double check the full code
Your complete myscript.py file should look exactly like this:
questions = [
("Which keyword starts a condition in Python? ", "if"),
("How many spaces are recommended per indentation level? ", "4"),
("Which keyword repeats over a sequence? ", "for"),
]
score = 0
for prompt, correct_answer in questions:
answer = input(prompt).strip().lower()
if answer == correct_answer:
print("Correct!")
score += 1
else:
print("Not quite. The answer is", correct_answer)
print("Final score:", score, "out of", len(questions))
Your quiz now asks every question plus tracks the correct answers. Next, you will wrap these blocks in a reusable function.
Organize the Quiz as a Function
Your repaired loop now asks all three questions. It also reports one final score.
Its logic currently sits at the top level of the file. A function gives that logic a reusable outer block that one call can start.
In this step, get ready to:
- Place the quiz inside run_quiz().
- Preserve the deeper loop and conditional indentation levels.
- Start the organized quiz with one unindented function call.
Wrap the quiz in a function
The function header opens a new block around the entire quiz. Every existing line moves four spaces deeper while keeping its relative nesting.
- Switch back to myscript.py in Notepad.
- Replace the current contents of myscript.py with this function definition:
def run_quiz():
questions = [
("Which keyword starts a condition in Python? ", "if"),
("How many spaces are recommended per indentation level? ", "4"),
("Which keyword repeats over a sequence? ", "for"),
]
score = 0
for prompt, correct_answer in questions:
answer = input(prompt).strip().lower()
if answer == correct_answer:
print("Correct!")
score += 1
else:
print("Not quite. The answer is", correct_answer)
print("Final score:", score, "out of", len(questions))
How does the nesting work?
- The def run_quiz(): header opens the function block.
- The question list starts four spaces inside the function.
- The for header also starts four spaces inside the function.
- The answer input and conditional start eight spaces inside the function.
- The feedback lines start twelve spaces inside the function.
- The final score returns to four spaces. This places it outside the loop while keeping it inside the function.
Start the function with one call
A function definition stores the quiz instructions under one name. An unindented call at the left edge starts those instructions.
- Place your cursor after the final print("Final score:", score, "out of", len(questions)) line.
- Press Enter three times to leave two blank lines after the function.
- Start the quiz by adding this unindented call:
run_quiz()
Why is the call unindented?
The left-edge position places run_quiz() outside the function body. Python reaches this line and starts the function once.
- Save the changes to myscript.py in Notepad.
- Switch back to the same Command Prompt from earlier.
Before you run the file, consider whether the final score line appears once or three times.
- Run the organized quiz by running this command:
python myscript.py
What should you see?
You should receive all three question prompts. Each answer should produce matching feedback.
The final score should appear once after the third answer. Your function now runs the complete quiz from one call.
Quiz not starting?
Check that run_quiz() is present after the function. Make sure it begins at the left edge.
If Python reports an indentation problem, check that every line in the function begins with at least four spaces.
Help me repair my function indentation.
✔️ Awesome, I've got everything!
Your saved myscript.py file now defines the quiz as a function and starts it with one call.
ⓧ I'd like to double check the full code
Your complete myscript.py file should match this version.
def run_quiz():
questions = [
("Which keyword starts a condition in Python? ", "if"),
("How many spaces are recommended per indentation level? ", "4"),
("Which keyword repeats over a sequence? ", "for"),
]
score = 0
for prompt, correct_answer in questions:
answer = input(prompt).strip().lower()
if answer == correct_answer:
print("Correct!")
score += 1
else:
print("Not quite. The answer is", correct_answer)
print("Final score:", score, "out of", len(questions))
run_quiz()
How do the blocks fit together?
The complete file has four visible indentation levels. Each deeper level belongs to the block opened by the nearest header above it.
Dedenting moves execution back to an outer block. The final run_quiz() call returns completely to the left edge.
Trace each indentation level
Tracing the left edges shows exactly which lines repeat or run conditionally. This makes the function structure easier to diagnose when you edit it later.
- Find def run_quiz(): at the left edge of myscript.py.
- Trace the function body at four spaces.
- Trace the loop body at eight spaces.
- Trace the conditional branch bodies at twelve spaces.
- Confirm that the final run_quiz() call returns to the left edge.
Before the final run, predict which indentation levels repeat for every question.
- Run the complete quiz from the same Command Prompt by running this command:
python myscript.py
Your final check
You should answer all three questions and receive feedback after each one. You should then see Final score: followed by the correct score out of 3.
That completes the refactor. One unindented call now starts your reusable quiz function.
Seeing an indentation problem?
An IndentationError means one line does not form a valid block. Compare that line's leading spaces with the surrounding lines that share its owner.
Check that the final score has four spaces. Check that the last run_quiz() call has none.
Help me trace this indentation error.
Secret mission
Add Tiered Score Feedback
Your quiz already reports a number. Add tiered feedback with a nested condition. Prove that every score range gets one matching message.
Clean Up Your Resources
Clean Up Your Resources
This project stores everything in the local myscript.py file at no ongoing cost. Choose the cleanup option that matches what you want to do next.
Resources you used:
- One local myscript.py file in the folder you used for this project.
Keep everything running
No action is needed. Choose this option if you want to rerun the quiz or practise more indentation changes.
- Your myscript.py file stays in its current folder at no cost.
- Your quiz remains ready to edit in Notepad.
- Your quiz remains ready to run from the Command Prompt folder you used earlier.
Pause - I'll come back to this later
Close the tools from this project while keeping your saved quiz. You can return to the same file later.
- Close the Notepad window from earlier.
- Close the Command Prompt window from earlier.
Your saved myscript.py file remains unchanged in its current folder.
Delete - I don't want to use this again
Deleting myscript.py is permanent. Your Python installation stays untouched.
- Press the Windows key to open the search bar.
- Type File Explorer.
- Press Enter to open File Explorer.
- Return to the folder that contains myscript.py.
- Select myscript.py.
- Press Shift+Delete to permanently remove the file.
- Confirm the deletion if Windows asks.
- Confirm that myscript.py no longer appears in the folder.
Nice Work!
Nice Work!
You did it! Your reusable Python quiz now guides a player from three questions to score-based feedback.
You've learned how to:
- Built a three-question terminal quiz that normalizes typed answers before checking them.
- Used four-space indentation to keep the loop inside run_quiz(). Nested each answer branch one level deeper.
- Triggered an IndentationError in the loop body. Repaired it by tracing the failure to its source line.
- Secret Mission: Added one branch for perfect scores. Nested another condition to separate near-perfect scores from lower scores.
Ready to quiz yourself?