Two people working a test machine with FAIL and PASS lamps and a RUN TESTS lever while a glowing robot mentor hands one of them a HINT card.

Learn With Mentor AI: Working Through a Coding Exercise

by Bartosz Zaczyński Updated Reading time estimate 20m basics ai python

Real Python has hundreds of coding exercises that let you practice what you’ve learned in a tutorial or course. Each exercise gives you a task, a code editor, and a set of tests that check your solution. When all the tests pass, you’ve solved the exercise.

Sometimes, though, the tests keep failing and you can’t figure out why. At that point, it’s tempting to open the solution and copy it. The tests turn green, but you don’t learn much from it.

Mentor AI is an AI tutor built into the Real Python platform. It can help you get unstuck on a coding exercise without handing you the answer. You open it by clicking the round orb in the bottom-right corner of any page or using the Ctrl+M shortcut. If the orb covers something you want to read, then you can drag it anywhere on the screen:

Mentor AI knows which tutorial, lesson, or exercise you’re looking at. On an exercise page, it can also look at the code you’ve run and your test results, so you don’t have to copy them into the chat. You can ask it to explain a failing test, give you a hint instead of the answer, or check whether you’ve understood an idea.

In this tutorial, you’ll learn how to:

  • Run the tests and read the results before asking for help
  • Ask Mentor AI why a test failed without pasting any code
  • Check your understanding by predicting what the code does
  • Ask for a hint about a specific failing test instead of the full solution
  • Request an example when an explanation isn’t enough
  • Get your solution reviewed, explain it back, and take a short quiz

Take the Quiz: Test your knowledge with our interactive “Learn With Mentor AI: Working Through a Coding Exercise” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Learn With Mentor AI: Working Through a Coding Exercise

Practice getting unstuck on a Python coding exercise with Mentor AI, from mutable default arguments to checking for None with is.

You’ll practice all of this by working through one real exercise, Fix the Sticky Default List, step by step. Each step shows what to click, what to type, and what Mentor AI said in an example session.

Prerequisites

To follow along with this tutorial, you’ll need the following:

You’ll work on the Fix the Sticky Default List exercise from the Python’s None: Null in Python course. Open it in a new browser tab now so you can switch between the exercise and this tutorial.

The exercise gives you a function called add_element(). It’s supposed to append an item to the list you pass in and return that same list, so the caller’s own list changes too. If you don’t pass a list, then it should start with a new, empty one. Here’s the starter code that you’ll see in the editor:

Language: Python
def add_element(new_elem, starter_list=[]):
    """Append new_elem to starter_list and return the list."""
    starter_list.append(new_elem)
    return starter_list

Here’s what happens when you call this function twice without passing a list. You don’t need to run these calls yourself. They just show the bug:

Language: Python
>>> add_element(1)
[1]
>>> add_element(2)
[1, 2]

The second call should return [2], but the 1 from the first call is still there. That’s the bug, and your job is to fix it. If you already know how, that’s fine. Follow the steps anyway to see how Mentor AI can help you.

Step 1: Run the Tests in the Coding Exercise

Before you ask for help, find out what’s wrong. Above the code editor on the exercise page, you’ll find two buttons: Reset and Run Tests. Click Run Tests without changing any code:

Coding exercise editor header with the solution.py filename next to the Reset and Run Tests buttons
The Reset and Run Tests Buttons Above the Code Editor

The Test Results panel on the right side of the page shows the outcome. Each test has a short name that describes what it checks, such as:

starter_list no longer has a mutable default

A mutable object is one that you can change in place, the way .append() changes a list. In contrast, an immutable object, such as None, can’t be changed. Using a mutable default is risky because every call that relies on it changes the same object, which is exactly the bug you saw above.

For the starter code, all six tests fail:

Test Results panel showing 0 passed and 6 failed for the starter code, with the first failed test expanded to show its message and hint
The Test Results Panel With All Tests Failing

Click a failed test to expand it. You’ll see three things:

  1. A message that explains what went wrong
  2. A short hint written by the exercise author
  3. A link labeled Explain why this test failed

Read the message and the hint first. Sometimes they’re enough to get you going. If they aren’t, then move on to the next step.

Step 2: Ask Mentor AI Why a Test Failed

Under the first failed test, click Explain why this test failed. This opens the Mentor AI chat panel and sends a question about that test for you. The test’s message is attached to your question, so Mentor AI knows which test you mean.

Before Mentor AI answers, it shows a few short status lines, such as Checking what’s on your page and Looking at your exercise attempts. These tell you what Mentor AI is reading to answer your question. Here’s what the chat panel looked like in the example session:

Real Python coding exercise page with failing tests on the right and the Mentor AI chat explaining why the first test fails
Mentor AI Explaining a Failing Test

Here’s Mentor AI’s reply:

This test inspects the function’s declared default, not what happens when you call it. Your signature still has starter_list=[], so the test sees a mutable list and fails before checking anything else.

Use the immutable “no value” marker, None, as the default. Change that first, then run the tests again.

Notice that you didn’t paste your code or the test output into the chat. Mentor AI looked them up on its own because it knows which exercise you’re working on.

The chat panel stays open next to the exercise. You can make it smaller with the arrow button in its top-right corner and open it again with the round Mentor AI button. Your conversation stays where you left it.

Step 3: Check Your Understanding Before You Change the Code

At this point, you could change the default value right away. But first, make sure that you understand why the code is wrong. A good way to do that is to predict what the code will do and ask Mentor AI to check your prediction.

This is a separate thought experiment, so forget the 1 and 2 from earlier. Imagine that the starter function has just been defined and its default list is still empty. Without running any code, decide what it returns for each of these three calls, made one after another:

Language: Python
add_element(5)
add_element(6)
add_element(7, [])

Then type your prediction into the box at the bottom of the chat panel and press Enter. Ask Mentor AI to check your reasoning without running the code:

Starting from a freshly defined function, my guess: add_element(5) gives ___, then add_element(6) gives ___, and add_element(7, []) gives ___. Am I right? Don’t run it yet, just tell me if my reasoning holds.

Once you’ve made your own prediction, expand the box below to compare it with the example session:

The message from the example session was:

My guess: add_element(5) gives [5], then add_element(6) gives [5, 6], and add_element(7, []) gives [7]. Am I right? Don’t run it yet, just tell me if my reasoning holds.

Mentor AI replied:

Yes, that reasoning holds for the current version with starter_list=[]. The first two calls share the default list, so it grows from [5] to [5, 6]. The third call passes a separate empty list explicitly, so it returns [7] without changing the shared default.

If your prediction is wrong, Mentor AI will tell you which part is off. That’s useful too, because now you know exactly what to learn.

Next, ask why Python behaves this way. You can tell Mentor AI how long or how simple you want the answer to be. For example, type this message:

Why does Python only create that default list once? Give me the short version with an everyday analogy, two or three sentences max.

Mentor AI replied:

Python evaluates default arguments when it executes the def statement, then stores them on the function for later calls. It’s like putting one shared bowl beside a recipe card: every cook using the default reaches for that same bowl.

This matches the Python language reference, which says that default values are evaluated once, when Python defines the function. Every call that relies on the default gets that same list object:

The add_element function stores one default list, and the calls add_element(1) and add_element(2) both append to it.
One Default List Shared by Every Call

If an answer is too long or too hard to follow, ask for a shorter or simpler one. Want to dig deeper?

Step 4: Select a Failing Test and Ask for a Hint

Now you can try to fix the code. A common first attempt is to use None as the default value and create a new list when the argument is empty. Replace the code in the editor with this version:

Language: Python
def add_element(new_elem, starter_list=None):
    """Append new_elem to starter_list and return the list."""
    if not starter_list:
        starter_list = []
    starter_list.append(new_elem)
    return starter_list

Click Run Tests again. This time, five tests pass, but the test named Uses a passed-in empty list still fails. Its message says Expected: ['a'] and Got: [].

That message can be confusing because the function clearly appends 'a' before it returns. The key is that the test doesn’t check the returned list. It passes in its own empty list and then checks that original list. This is roughly what the test does, and again, you don’t need to run it:

Language: Python
>>> my_list = []
>>> add_element("a", my_list)
['a']
>>> my_list
[]

The function returns ['a'], but my_list, the list that the caller passed in, is still empty. Your job is to find out why.

You can ask Mentor AI about any text on the page, including a test message. Use your mouse to select the failed test’s message. A small Ask Mentor AI button pops up next to the selection:

Click Ask Mentor AI. The chat panel opens with the selected text attached to your next message. Now type your question. If you only want a hint and not the solution, then say so clearly:

I changed the default to None and everything passes except this one. Give me a hint, not the fix.

Here’s the reply from the example session:

Mentor AI chat with a quoted test failure and a reply that names the problem and asks a guiding question instead of giving the fix
Mentor AI Giving a Hint Instead of the Fix

Mentor AI pointed you to the empty-list case, but it didn’t write the corrected code. Instead, it asked you questions to think through:

When the caller passes [], what does if not starter_list evaluate to? Can that condition distinguish an empty list from None?

Before you read on, try to answer both questions yourself and update the code. If you’re still stuck, ask Mentor AI for another hint. When you have your answers, expand the box below to check them:

An empty list is falsy, so if not starter_list evaluates to True when the caller passes [], just like it does for None. The function then rebinds its local starter_list parameter to a new list and appends to that instead, leaving the caller’s original list untouched. The caller’s list stays empty, which is why the test reports Got: [].

So no, the condition can’t tell an empty list apart from None. Both are falsy, and not only checks truthiness.

Step 5: Ask for an Example When an Explanation Isn’t Enough

To tell None apart from an empty list, you need to compare the argument to None directly. There are two ways to write that comparison:

  • starter_list == None
  • starter_list is None

If you wrote if starter_list is None:, then all six tests pass, and so does an extra test marked Bonus. You can still follow this step to find out why your choice matters. If you wrote if starter_list == None:, as in the example session, then all six tests pass, but the bonus test, called Checks for None with the identity operator, still fails:

Test Results panel showing all six tests passed while the bonus identity operator test still fails
All Six Tests Passing With the Bonus Test Still Failing

Since == seems to work, you might wonder why the bonus test cares. Ask Mentor AI:

All six tests pass now with starter_list == None, but the bonus test fails. Both == and is seem to work, so why does it matter which one I use?

If you already used is, then ask why is None is better than == None instead.

Mentor AI first confirmed that your required tests pass. Then it explained that the bonus test expects an identity check. Because None is a singleton, meaning that there’s only one None object in Python, is None is the conventional test. It also offered follow-up buttons, such as Explain is None vs == None, that you can click instead of typing your next question.

In other words, == checks equality, which asks whether two objects consider themselves equal. Each object gets to decide that for itself. The is operator, on the other hand, checks identity by testing whether two names point to the exact same object.

That still doesn’t show how == could ever go wrong. When an explanation leaves you with a question like this, ask for an example:

Can you show me a tiny example where == None would actually give the wrong answer?

Mentor AI replied with this short example:

Language: Python
class LooksLikeNone:
    def __eq__(self, other):
        return other is None

value = LooksLikeNone()
print(value == None)  # True
print(value is None)  # False

This example uses a class, but you don’t need to know how to write classes to follow it. When Python evaluates value == None, it calls a special method named .__eq__() on value and uses whatever that method returns. The LooksLikeNone class defines its own .__eq__(), which changes what == does for its objects. It returns True whenever the other object is None, so value == None returns True even though value isn’t None.

The is check isn’t fooled because no object can change what is does. That’s why PEP 8, Python’s style guide, recommends is None over == None.

If you used ==, then change the condition to if starter_list is None: and run the tests. All six tests and the bonus test pass. If you’d like to learn more about the difference between the two operators, then ask Mentor AI from here:

Step 6: Get Your Solution Reviewed and Explain It Back

Passing tests tell you that your code does what the tests check. They don’t tell you whether your code is clear or easy to read. Before you move on, ask Mentor AI to review your solution. To keep the review short and your code your own, say exactly what you want:

All green, bonus too. Can you review my solution? Tell me one thing I did well and one thing I could improve, but don’t rewrite it for me.

In the example session, Mentor AI said that using None as the default with an is None check was a good choice. It gives you a fresh list when you need one and still appends to the exact list that the caller passes in, even an empty one. Then Mentor AI suggested one improvement:

Add type annotations to clarify the contract: starter_list may be a list or None, but the function always returns a list.

Type annotations are optional labels that tell readers and tools what kinds of values a function expects and returns. Python doesn’t enforce them, and the tests pass with or without them, so adding them is up to you. If you haven’t used them before, then ask Mentor AI to explain the suggestion before you decide.

Next, check that you can explain the fix in your own words. Write a short explanation and ask Mentor AI whether it’s correct. Here’s the message from the example session, which contains a mistake:

Let me explain it back to check I really get it: the old version shared one list because Python builds the default once. None works as a default because it’s immutable, so Python makes a new None on every call, and then I create the list inside the function. Is that right?

Mentor AI found the mistake and corrected it:

You’ve got the main idea right. Small correction: Python doesn’t create a new None on each call. None is a singleton, and the function safely reuses it because it’s immutable. The [] expression inside the function runs each time the function is called with None, creating a fresh list.

Explaining something in your own words is a good way to find gaps in your understanding. If Mentor AI corrects you, read the correction and try explaining it again.

Step 7: Take a Quick Quiz

As the last step, ask Mentor AI for a quiz question to check that you understand why your fix works:

Quiz me with one quick question so I know this stuck.

Mentor AI writes a new question every time, so yours will probably test a different point or offer different options. Answer it by clicking one of the options. Mentor AI then tells you whether you’re right and explains why, so rely on that feedback for your own question.

In the example session, Mentor AI asked a multiple-choice question about how the fixed function treats an empty list:

Mentor AI chat showing a multiple-choice quiz question about mutable default arguments with four clickable answers
A Multiple-Choice Quiz Question in Mentor AI

If you’re reading along without the exercise open, then choose an option for this example question and expand the box below to check it:

For the example question in the screenshot, the correct answer is D. The passed list isn’t None, so it isn’t replaced. The if starter_list is None: check is true only when the caller leaves out the argument or explicitly passes None. An empty list is a real list object, not None, so the function keeps it and appends to it.

To get more questions on this topic, click the button below:

Want more practice?

Next Steps

You can follow the same steps for any Real Python coding exercise. The next time your tests fail, go through this checklist:

  1. Run the tests and read the failure messages and hints.
  2. Click Explain why this test failed if the message isn’t clear.
  3. Predict what your code does and ask Mentor AI to check your prediction.
  4. Select a failing test message, click Ask Mentor AI, and ask for a hint, not the fix.
  5. Ask for an example when an explanation is hard to follow.
  6. Once the tests pass, ask for a review, explain your fix back, and ask for a quiz question.

To keep this checklist and the example prompts at hand, download the free cheat sheet from the Learn With Mentor AI Kit:

Real Python Mentor AI Coding Exercises Cheat Sheet pages stacked on top of each other

Free Learn With Mentor AI Kit: Video, Cheat Sheets, Course, and Quiz

Learn Python faster by working through it in conversation with Mentor AI, and focus your practice on the Python skills employers look for.

You don’t need special commands for any of this. Write your questions in plain English, and tell Mentor AI what kind of help you want.

To practice right away, pick an exercise course on a similar topic:

If you’d like a suggestion based on what you’ve already done, then you can also ask Mentor AI:

Beyond coding exercises, you can also use Mentor AI to learn from video courses and to work through async Python one question at a time.

Conclusion

In this tutorial, you’ve learned how to use Mentor AI to work through a Real Python coding exercise without copying the solution. You fixed a function with a mutable default argument by asking for explanations, hints, and examples instead of the answer. Along the way, you checked your understanding by predicting what the code does, explaining your fix back, and answering a quiz question.

Mentor AI is most helpful when you tell it what kind of help you need. Open any coding exercise, run the tests, and give it a try.

Take the Quiz: Test your knowledge with our interactive “Learn With Mentor AI: Working Through a Coding Exercise” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Learn With Mentor AI: Working Through a Coding Exercise

Practice getting unstuck on a Python coding exercise with Mentor AI, from mutable default arguments to checking for None with is.

🐍 Python Tricks 💌

Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Dictionary merging in Python 3.5+

About Bartosz Zaczyński

Bartosz is an experienced software engineer and Python educator with an M.Sc. in Applied Computer Science.

» More about Bartosz

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Master Real-World Python Skills With Unlimited Access to Real Python

Locked learning resources

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

Master Real-World Python Skills
With Unlimited Access to Real Python

Locked learning resources

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal.


Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!