Your Python Code Runs. But Is It Actually Correct?
When you're learning Python, error messages feel like the enemy.
Red text appears. Your program stops. And all you want is to make the error disappear.
So when your code finally runs without errors, it feels like you've succeeded.
But what if I told you that your code could run perfectly… and still give you the wrong answer?
Let's look at a real example.
The Setup
Imagine you're building a program that checks whether someone is an adult.
Your first attempt might look like this:
age = input("Age: ") if age >= 18: print("Adult") Run it and enter 9.
Python gives you an error:
TypeError: '>=' not supported between instances of 'str' and 'int' Why?
Because input() always returns text (a string), even when someone enters a number.
Now imagine someone tries a quick fix.
Python doesn't want to compare text with a number?
Fine. Let's put quotes around 18.
The error disappears.
But we've just created a much more interesting problem.
Your Turn: Predict the Output
Look carefully at this code:
age = "9" if age >= "18": print("Adult") else: print("Minor") What do you think Python will print?
- A.
Adult - B.
Minor - C.
TypeError - D. Nothing prints
Take a moment to think before revealing the answer.
Reveal the answerThe correct answer is A: Adult.
Yes. Python classifies a 9-year-old as an adult.
And the surprising part?
There is absolutely no error.
If you chose B, your reasoning makes sense.
A human sees the number 9 and knows it's smaller than 18.
But Python isn't comparing numbers here.
Why Is "9" Greater Than "18"?
Look closely at the quotation marks.
age = "9" This is a string, not an integer.
And here:
age >= "18" Python is comparing two strings.
When Python compares strings, it checks their characters from left to right.
Let's break it down:
- First, Python compares
"9"with"1". - These characters are different.
- Python makes its decision immediately.
- It never needs to look at the
"8".
In Python's character ordering, "9" comes after "1".
Therefore:
print("9" >= "18") Output:
True You can see the numerical values behind these characters using ord():
print(ord("9")) # 57 print(ord("1")) # 49 Since 57 is greater than 49, "9" is considered greater than "18".
Python isn't thinking about ages.
It's comparing text.
The Sneaky Part: Sometimes It Works!
This is what makes the bug dangerous.
Let's test different ages:
| Age | Program says | Correct? |
|---|---|---|
"5" | Adult | No |
"9" | Adult | No |
"17" | Minor | Yes |
"18" | Adult | Yes |
"25" | Adult | Yes |
"100" | Minor | No |
Notice something?
If you only test 17, 18, and 25, your program appears to work perfectly.
But it fails with 5, 9, and 100.
That's why testing only normal-looking values isn't enough.
The Fix: Convert Text Into a Number
The real problem is that input() returns a string.
We need to convert that string into an integer before comparing it.
Here's the correct version:
age = int(input("Age: ")) if age >= 18: print("Adult") else: print("Minor") Now Python compares actual numbers.
Enter 9:
Age: 9 Minor That's the result we expected!
You can also check the type yourself:
age = "9" print(type(age)) Output:
<class 'str'> And after conversion:
age = int("9") print(type(age)) Output:
<class 'int'> One important detail: int() expects text that represents a whole number.
For example:
int("nine") This raises a ValueError because "nine" cannot be converted into an integer.
The Bigger Lesson: No Error Doesn't Mean No Bug
A crash is frustrating, but at least it tells you something went wrong.
A silent bug is different.
Your program runs. It prints an answer. Everything looks fine.
But the answer is wrong.
Here are three habits that can help you avoid these mistakes:
1. Understand your data types.
When something behaves unexpectedly, check its type using type().
Remember: data from input() is text by default.
2. Fix the cause, not just the error message.
Don't change your code just to make an error disappear.
Understand what Python is telling you first.
3. Test more than one situation.
Try normal values, unusual values, and boundary values.
For this example, testing 17, 18, and 100 would reveal problems that a single test might miss.
Getting your code to run is only the first checkpoint.
The real goal is to make sure it does what you intended.
Your Turn!
Did you predict Adult before opening the answer?
Tell me in the comments!
And if you want to practice thinking this way, explore Python Builder — World 1: The Awakening.
It's a challenge-based Python workbook where you investigate broken code and develop your problem-solving skills across eight missions.
Start building: Python Builder — World 1
CodeStra — Making Python Click.
原文:https://www.a8q9.cn/en/article/your-python-code-runs-but-is-it-actually-correct-02db67.html