Python 8.3: Refactoring code

Our quiz already works, but the code is heavily duplicated: each question is implemented inside its own function. If we had 10, 50 or 100 questions, copying the same logic that many times would be painful and error-prone. The exercise here is to refactor the program by writing a single reusable function called ask_question that takes everything that differs between questions as parameters.

Designing the ask_question function

The pieces that change from one question to the next are the question text, the four possible answers, and the letter of the correct one. So our function will take six parameters: question, r1, r2, r3, r4 and choice_answer. Inside the function we print the question and the four answers, ask the user to type their letter, and compare it with the expected one.

def ask_question(question, r1, r2, r3, r4, choice_answer):
    print(question)
    print("a: " + r1)
    print("b: " + r2)
    print("c: " + r3)
    print("d: " + r4)
    answer = input("Your answer: ")
    if answer == choice_answer:
        print("Good answer")
    else:
        print("Wrong answer")

ask_question("What is the capital of France?",
             "London", "Berlin", "Paris", "Madrid", "c")
ask_question("What is the capital of Italy?",
             "Rome", "Venice", "Florence", "Pisa", "a")

Now adding a new question costs a single line instead of a whole new function. Running the code we go through both questions, type our answers and the program tells us each time whether they are correct. The old per-question functions can be deleted entirely. This refactoring step is exactly the kind of cleanup you will do every time you notice that the same logic is repeated with slightly different data. In the next video we will add a score on top of this new structure.

Summary

This lesson teaches code refactoring using a practical quiz application example. The instructor demonstrates how repetitive code for multiple quiz questions can be consolidated into a single reusable `ask_question()` function. The key principle is that instead of copying and pasting similar code blocks for each question, you create one parameterized function that handles all cases, improving maintainability and scalability. This approach eliminates the burden of maintaining duplicate logic across many questions.

Key points

  • Code refactoring eliminates duplication by consolidating repetitive patterns into reusable functions, reducing copy-paste code blocks
  • A parameterized function accepts different questions and answer choices as arguments instead of hardcoding them for each quiz item
  • Scaling benefits: adding 10, 100, or more questions requires only adding function calls rather than copying entire code sections
  • The DRY principle (Don't Repeat Yourself) improves code quality, maintainability, and reduces the risk of inconsistent behavior
  • Refactored code is easier to debug and update since changes to the question logic only need to be made in one place
  • The lesson uses a multiple-choice quiz with questions about European capitals to demonstrate the practical refactoring workflow

FAQ

Why is refactoring better than copying code multiple times?

Refactoring creates a single source of truth. When you need to change how questions are asked or processed, you only modify the function once instead of updating the same logic scattered across multiple code blocks.

How does the refactored ask_question() function handle different quiz questions?

The function uses parameters to accept different question text, answer choices, and correct answers for each call. The underlying logic remains constant while the input data changes, making it reusable for any quiz question.

What is the practical advantage when scaling from 3 questions to 100 questions?

With a refactored function, you can handle 100 questions by simply making 100 function calls with different parameters. Without refactoring, you would need to write or copy the entire code block 100 times, making it error-prone and difficult to maintain.