Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help With Error Handling
#1
Hello - I am new to python and am attempting to validate that the user input is a number with my script. I have the below syntax, but instead of actually presenting my expected message, I get the below...

What do I need to change in my python syntax in order to get my desired error message?

Error:
Traceback (most recent call last): File "<ipython-input-8-cfa0bf62612b>", line 1, in <module> runfile('/home/owner/.config/spyder/temp.py', wdir='/home/owner/.config/spyder') File "/usr/local/lib/python2.7/dist-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "/usr/local/lib/python2.7/dist-packages/spyder_kernels/customize/spydercustomize.py", line 100, in execfile builtins.execfile(filename, *where) File "/home/owner/.config/spyder/temp.py", line 24, in <module> age = inputage("How old are you?") File "/home/owner/.config/spyder/temp.py", line 16, in inputage userInput = int(input(message)) File "/usr/local/lib/python2.7/dist-packages/ipykernel/ipkernel.py", line 176, in <lambda> builtin_mod.input = lambda prompt='': eval(self.raw_input(prompt)) File "<string>", line 1, in <module> NameError: name 's' is not defined
And this is the syntax that I am trying:
def inputage(message):
  while True:
    try:
       userInput = int(input(message))       
    except ValueError:
       print("Not an integer! Try again.")
       continue
    else:
       return userInput 
       break 
     
age = inputage("How old are you?")

if (age>=18):
  print("You are over 18")
else:
  print("You are under 18")
Reply


Messages In This Thread
Help With Error Handling - by jo15765 - Sep-14-2018, 02:44 PM
RE: Help With Error Handling - by Vysero - Sep-14-2018, 03:48 PM
RE: Help With Error Handling - by volcano63 - Sep-14-2018, 03:48 PM
RE: Help With Error Handling - by gruntfutuk - Sep-14-2018, 04:53 PM
RE: Help With Error Handling - by jo15765 - Sep-14-2018, 06:04 PM
RE: Help With Error Handling - by snippsat - Sep-14-2018, 06:20 PM
RE: Help With Error Handling - by jo15765 - Sep-14-2018, 06:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculating Average with Error Handling mikasa 7 698 May-07-2024, 07:48 AM
Last Post: snippsat
Star python exception handling handling .... with traceback mg24 3 1,383 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Help needed with a "for loop" + error handling tamiri 2 2,765 May-27-2022, 12:21 PM
Last Post: tamiri
  Handling Python Fatal Error richajain1785 7 6,171 Oct-14-2021, 01:34 PM
Last Post: Tails86
  Error Handling JarredAwesome 5 3,104 Oct-17-2020, 12:41 AM
Last Post: JarredAwesome
  Error handling using cmd module leifeng 3 3,024 Jun-06-2020, 06:25 PM
Last Post: leifeng
  Excpetion Handling Getting Error Number gw1500se 4 2,528 May-29-2020, 03:07 PM
Last Post: gw1500se
  Warning / Error handling in python Prarthana_12 1 5,202 Feb-08-2019, 09:21 PM
Last Post: snippsat
  Error Handling/No results from SQL Query JP_ROMANO 7 9,797 Jul-18-2018, 02:31 PM
Last Post: JP_ROMANO
  Error handling parthi1705 0 2,048 Jun-13-2018, 11:57 AM
Last Post: parthi1705

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020