Safe Number Converter
Beginner · Write the code · from Python Type Conversion: How to Convert Between Data Types
From the lesson: Python Type Conversion: How to Convert Between Data Types
Imagine you ask someone their age and they write it on a sticky note. You now have the text "25" - but you can't do math with a sticky note. You need to read the text and turn it into an actual number in your head first.
Read the lessonWrite code that checks three strings using .isdigit() and converts them to integers only if safe. For each string, print either the converted number or "Not a number".
Given:
values = ["42", "hello", "100"]Expected output:
42
Not a number
100values = ["42", "hello", "100"]
# Loop through values
# If the value is a digit, convert and print the integer
# Otherwise print "Not a number"
More Type Conversion exercises
All Python exercises · 50 practice problems · Python Fundamentals quiz