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

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:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

Locked learning resources

The full article is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

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:

What Do You Think?

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!

Become a Member to join the conversation.

Keep Learning

Related Topics: basics ai python