Define ask_question
Extract the question display, input, and feedback into a function
Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.
The for loop body handles everything: it displays the question, waits for input, checks the answer, and prints feedback. That is four separate jobs packed into one block. Extracting them into a function makes the loop body shorter and the function independently testable.
Defining ask_question
The function needs all the question data as parameters: the question text, the four options, and the correct answer. The body does exactly what the loop body does now — display, get input, check, print feedback:
def ask_question(text, o1, o2, o3, o4, correct):
print(text)
print()
print(f" 1) {o1}")
# ... options 2, 3, 4 ...
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}.")Where to define it
Functions must be defined before they are called. Define ask_question after QUESTIONS and before the while loop.
The while loop stays unchanged
You are not calling the function yet — that comes in the next chapter. For now, define the function and leave the while loop exactly as it is.
Instructions
You are moving the question logic out of the loop and into a dedicated function. The function needs all six pieces of question data as parameters — the same values the loop currently unpacks from each tuple.
- After the
QUESTIONSlist andtotal = 3, define a function namedask_questionwith parameterstext,o1,o2,o3,o4, andcorrect. Defining it here ensures it exists before thewhileloop calls it. - Inside the function, call
print(text), thenprint()— this displays the question followed by a blank line for readability. - Call
print(f" 1) {o1}"),print(f" 2) {o2}"),print(f" 3) {o3}"), andprint(f" 4) {o4}"), each on its own line — this displays the four answer options. - Call
print(), then assigninput("Your answer (1-4): ")toanswer_text— the blank line separates the options from the prompt visually. - Assign
int(answer_text)toanswer— this converts the player's text input into a number so it can be compared tocorrect. - Assign
answer == correcttois_correct— this evaluates the comparison and stores the result so you can branch on it. - Call
print(), thenprint(f"You answered: {answer}")— the blank line separates the prompt from the feedback. - Write
if is_correct:and callprint("Correct!")inside it. Add anelseclause that callsprint(f"Wrong! The correct answer was option {correct}.")— this gives the player immediate feedback on their answer.
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
# Step 1: Define ask_question(text, o1, o2, o3, o4, correct):
# Step 2: print(text), print()
# Step 3: print all four options using f-strings
# Step 4: print(), then answer_text = input("Your answer (1-4): ")
# Step 5: answer = int(answer_text)
# Step 6: is_correct = answer == correct
# Step 7: print(), then print(f"You answered: {answer}")
# Step 8: if is_correct: print "Correct!". else: print wrong message.
while True:
score = 0
streak = 0
print("=== Python Quiz ===")
print()
for text, o1, o2, o3, o4, correct in QUESTIONS:
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:
score = score + 1
streak = streak + 1
print("Correct!")
if streak >= 3:
print(f" Hot streak! ({streak} in a row)")
else:
streak = 0
print(f"Wrong! The correct answer was option {correct}.")
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