Why isn't my Python code working?
If you're running into a frustrating situation where it seems that your solution isn't working, don't worry. This can happen to anyone. While there is always the possibility that something may be wrong with the platform or an error in the course, the more likely reason is that there's a small mistake in your solution. To help determine what is the actual cause, this resource is for you.
How do Challenges work?
Each step of a Python Challenge runs a set of automated tests that check whether your code meets the step’s requirements. These tests examine both the structure and the output of your program. Every time you press the Run button, the tests run behind the scenes.
If your code passes all the tests, we’ll move you to the next step. If any tests fail, we’ll show you the most relevant hint based on your current code.
Accepted solutions
Challenge tests are designed to accept many different correct solutions, but they still rely on precise criteria. If the instructions ask you to print something specific, your output must match exactly. The test might check for a particular string or a close variation of it.
A few tips:
- Do not get ahead of the instructions. If something looks "off" but isn't mentioned yet, chances are we’ll fix it in a later step.
- Stick to the current step’s instructions. Making changes outside the scope of the task may cause the tests to fail.
- Get creative later. Once you complete the Challenge, feel free to experiment.
Staying within the instructions helps us help you and ensures the tests can evaluate your code properly.
Hints
Each time you run your code, we provide a hint based on which tests fail. But we do not include hints for every possible mistake.
If you keep seeing the same hint, it means the platform still sees that requirement as unmet, even if you've fixed other parts of your code.
Hints are there to point you in the right direction. It’s up to you to read the output, compare it to the instructions, and debug from there.
"Oops, something went wrong!"
If you stray too far from the instructions or delete the starter code entirely, you might see a red error banner that says:
Debugging checklist
Understanding how Challenges work can help you debug more effectively. Here’s a checklist to walk through if your code isn’t passing:
Fix the errors
- Are there any syntax or runtime errors?
- Do you see any linter warnings?
- Is there an error message in the console when you run the code?
- When was the last time your code was working?
- Use the stack trace to locate the line causing the error.
Debug the code
- What did you expect the code to do? What is it doing instead?
- Add print statements to check variable values.
- Look for common issues like incorrect indentation or using
=instead of==. - Comment out sections of your code to isolate the problem.
- Read your code out loud.
- Trace it line by line.
- Take a break. Fresh eyes can help.
Even experienced programmers miss simple things. Be patient with yourself.
Check the requirements
- Have you re-read the instructions carefully?
- What do the hints tell you?
- Did you meet all the requirements?
- Did you test different inputs?
- Are there any typos in your output?
- Does your output match the formatting in the instructions?
- Did you change more than what was asked?
Attention to detail matters. A single misspelled word can mean the difference between success and failure.
Still stuck? Get help!
If you've walked through everything and you're still not sure what’s wrong, it’s time to ask for help:
- Talk to a friend, teacher, or family member.
- Ask our AI tutor Khanmigo, if available.
- Post in the Questions section under the Challenge or video.
- Visit the Khan Academy Help Center to report a problem.
Important: The community cannot see your Challenge code. Be sure to include:
- The relevant part of your code
- What you’re trying to do
- What you expected to happen
- What actually happened
The more context you provide, the easier it is for someone to help you out.
