30 Day code challenge-python
Mar 23, 2021
Day 16–Exceptions — String to Integer
Task
Read a string, S, and print its integer value; if S cannot be converted to an integer, print Bad String
.
Input Format
A single string, S.
Output Format
Print the parsed integer value of S, or Bad String
if S cannot be converted to an integer.
Explanation
Line 1: Gets input from the user
Line 2–4: Checks whether the input is an integer if so, then it prints the value
Line 5–6: If the input is a string it can’t be converted into an integer. Therefore, except block gets executed and “Bad String” gets printed.
Bonus Tip: We can also handle specific error , except ValueError. This will handle only the value error.
See you on day 17