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
  How to capture an error message TheHacker707 4 151 Feb-15-2020, 03:03 PM
Last Post: snippsat
  Error Message Coming Up When Running Code eddywinch82 0 242 Feb-10-2020, 11:48 PM
Last Post: eddywinch82
  Error message when trying to install Matplotlib in Python3.8 Transitionyte 5 471 Jan-22-2020, 11:52 PM
Last Post: Transitionyte
  How to get valid error message while invoking REST API through Python gollupraveen 0 152 Oct-04-2019, 07:15 PM
Last Post: gollupraveen
  Project, Reading Data from a spreadsheet. Error message!! Shafla 1 257 Sep-27-2019, 10:44 AM
Last Post: buran
  object is not subscriptable... Error Message? Shafla 3 824 Sep-25-2019, 12:27 AM
Last Post: buran
  Openpyxl error message. Help! Shafla 6 337 Sep-24-2019, 07:25 PM
Last Post: Shafla
  clueless about the error message findbikash 2 285 Sep-19-2019, 01:10 PM
Last Post: findbikash
  My class has name error message 357mag 3 245 Sep-04-2019, 03:29 PM
Last Post: snippsat
  error message on program launch DJPY1175 0 354 Jun-10-2019, 08:40 AM
Last Post: DJPY1175

Forum Jump:

Users browsing this thread: 1 Guest(s)