Python Forum

Full Version: How to send an error message
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm not quite sure how to structure this question so I'm going to use an example
Let's say I have a program that asks a user for a number which I am going to use later
, I would do something like this

number = int(input("Please give me a number: "))
answer = number + 4
print("Your number plus 4 is " + str(answer) )
However, if the user inputs the letter form of the number such as "two" or "three"
they would receive an error message. I want to be able to create an error message with a While True loop so that if they did type in the number in letter form, or if they input something incorrectly for a similar program wouldn't crash. Is there a way to do this?
Remember that int will raise a ValueError if the value passed to it can't be converted to an integer. So, catch the exception and handle it (if you don't know about exception handling with try and except, now would be a good time to learn).