Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to send an error message
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).

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Don't understand error message Milfredo 2 168 Aug-24-2020, 05:00 PM
Last Post: Milfredo
  what to do about this error message deric23 0 263 May-15-2020, 02:49 PM
Last Post: deric23
  pip error message Marijn 1 293 Apr-21-2020, 12:05 PM
Last Post: snippsat
  Adding second message to simple loop error PythonGainz 2 369 Apr-06-2020, 11:55 AM
Last Post: PythonGainz
  How to capture an error message TheHacker707 4 543 Feb-15-2020, 03:03 PM
Last Post: snippsat
  Error Message Coming Up When Running Code eddywinch82 0 479 Feb-10-2020, 11:48 PM
Last Post: eddywinch82
  Error message when trying to install Matplotlib in Python3.8 Transitionyte 5 1,229 Jan-22-2020, 11:52 PM
Last Post: Transitionyte
  How to get valid error message while invoking REST API through Python gollupraveen 0 351 Oct-04-2019, 07:15 PM
Last Post: gollupraveen
  Project, Reading Data from a spreadsheet. Error message!! Shafla 1 831 Sep-27-2019, 10:44 AM
Last Post: buran
  object is not subscriptable... Error Message? Shafla 3 2,432 Sep-25-2019, 12:27 AM
Last Post: buran

Forum Jump:

Users browsing this thread: 1 Guest(s)