Home βΊ Problems βΊ Converting invalid text to a number
Converting invalid text to a number
π Fix the bug Topic: strings Difficulty: medium Free
What this problem practises
Python strings are immutable sequences. "Fixing" a string always produces a new one, so the return value has to be captured; slicing rules apply, and every method β upper(), strip(), split(), join() β behaves differently from the in-place mutation you might be picturing.
This is a fix-the-bug problem: a short program is given in its broken state, and the task is to repair the code so it produces the required output. The interactive editor runs your fixed version in the browser and checks it for you β no setup, no local interpreter.
Difficulty: medium β expect to hold two interacting rules in your head at once, such as a boundary combined with a mutation.
The challenge
This should convert user input into a number and double it, but it crashes on certain input. Fix it.
The code runs in PyDebugβs in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.
The error it raises
Run the broken snippet and Python stops with this exact error: ValueError: invalid literal for int() with base 10: 'twenty'. Match it to the Python errors guide, then repair the line that produced it β knowing the message is half the fix.
The snippet
The interactive editor pre-fills exactly this code, broken and ready to repair:
age_text = "twenty"
age = int(age_text)
print(age * 2)Understand the rule
ValueError: invalid literal for int() with base 10 means int() received a string whose characters do not form a whole number β 'abc', an empty string, '12.5', or '42 ' with a stray character. Note the exception type: the argument's type (str) was fine, its value was not, which is exactly the difference between TypeError and ValueError.
How to trace it
- Read the rejected value inside the quotes in the message; the crash shows exactly what int() received.
- repr() the input before converting β invisible characters, commas, or currency symbols are common stowaways.
- Strip whitespace first, and decide the policy for decimals: int('12.5') always fails; use float() then round or truncate deliberately.
- If the input comes from a user or file, wrap only the conversion in try/except ValueError and handle the bad value explicitly.
Common wrong fixes
- Catching Exception broadly around a whole block, so real bugs get classified as bad user input.
- Testing with clean numeric strings only β real inputs contain empty strings, spaces, and thousands separators.
- Chaining int(float(s)) everywhere without deciding whether truncation is actually acceptable.
Rule to remember
int() parses only optional sign plus digits (after surrounding whitespace). Everything else raises ValueError β validate or try/except at the conversion point, and keep the except clause narrow.
π’ Learning path: Variables and Data Types
Problem 5 of 13 in this path. Work them in order β each one builds on the rule before it.
β Converting text that isn't a numberNext in path: abs() and type conversion β
More strings problems
Keep going
- Python debugging guide β a systematic method for finding bugs like this one fast.
- Browse every strings problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.