Return a Value
Add return is_correct so the caller can update the score based on the result
Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.
ask_question displays a question and prints feedback, but the for loop still needs to know whether the player was correct — to update score and streak. Right now, the function prints the feedback but doesn't communicate the result back to the caller.
Return sends a value back
Use return at the end of the function to send a value back to whoever called it:
def ask_question(text, o1, o2, o3, o4, correct):
# ... display, input, feedback ...
return is_correctThe caller can then use the result:
result = ask_question(text, o1, o2, o3, o4, correct)
if result:
score = score + 1Without return, the function returns None
If a function has no return statement, Python returns None. None is falsy — if None: is always False. That means if result: would never run, and every answer would look wrong even when the player is correct.
What changes in the for loop
Once ask_question returns is_correct, the for loop body shrinks from 14 lines to 8. The display, input, and feedback move inside the function. Only the score and streak logic stays in the loop.
The starter code has already made this update to the for loop — your job is to add the return statement that makes it work.
Instructions
Add a return statement to ask_question.
- At the very end of the
ask_questionfunction body, after theelseclause, addreturn is_correct— this sends the result back to the caller so theforloop can updatescoreandstreakbased on whether the player was correct. Withoutreturn, the function returnsNone, andif result:would never beTrue.
QUESTIONS = [
("What is the type of the value 3.14?", "int", "str", "float", "bool", 3),
("What keyword defines a function in Python?", "for", "def", "if", "class", 2),
("What does print() do?", "Stores a value", "Returns a number", "Displays output", "Reads input", 3),
]
total = 3
def ask_question(text, o1, o2, o3, o4, correct):
print(text)
print()
print(f" 1) {o1}")
print(f" 2) {o2}")
print(f" 3) {o3}")
print(f" 4) {o4}")
print()
answer_text = input("Your answer (1-4): ")
answer = int(answer_text)
is_correct = answer == correct
print()
print(f"You answered: {answer}")
if is_correct:
print("Correct!")
else:
print(f"Wrong! The correct answer was option {correct}.")
# Step 1: Add return is_correct here
while True:
score = 0
streak = 0
print("=== Python Quiz ===")
print()
for text, o1, o2, o3, o4, correct in QUESTIONS:
result = ask_question(text, o1, o2, o3, o4, correct)
if result:
score = score + 1
streak = streak + 1
if streak >= 3:
print(f" Hot streak! ({streak} in a row)")
else:
streak = 0
print(f"Score: {score}/{total}")
if score == total:
print("Perfect score!")
elif score >= 2:
print("Great job!")
elif score >= 1:
print("Not bad!")
else:
print("Keep practicing!")
print()
again = input("Play again? (y/n): ")
if again != "y":
break
print("Thanks for playing!")
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding