Python Forum
How to send an error message - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to send an error message (/thread-24451.html)



How to send an error message - Ewilliam51 - Feb-14-2020

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?


RE: How to send an error message - ndc85430 - Feb-15-2020

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).