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
#2
If you get that error when you run your code its not coming from the code you posted here. The code you posted here works just as you would expect.
Reply
#3
input in Python 2 evaluates your input as a Python expression - see the docs; looks like you entered s - and the interpreter looks for a variable named s to return its value
Output:
>>> s = 2 >>> input('enter "s"') enter "s"s 2 >>> input('enter integer ') enter integer 2 2
Since there's no variable with that name, you get that exception.

With input, you don't need type conversion - eval('2') yields integer value of 2, e.g. - but input is considered unsafe (you may get malicious string to execute from user), so use raw_input with type conversion

PS Since you are starting with Python, I recommend to learn the Pythonic way to name objects (see PEP-8). When in Python - do as Pythonistas do Smile
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
jo15765 I am curious as to why you are using Python 2.7 - this is legacy python and should not normally be used by beginners of Python. Official support for 2.x ends on Jan 1, 2020.

Only people responsible for maintaining old code or needing to use some unique library that has not been updated yet should be using it.

I appreciate that there is some learning material that is still around using Python 2.x, but even then it is better to try to write in Python 3 and look up the off exception when you have a problem.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#5
(Sep-14-2018, 04:53 PM)gruntfutuk Wrote: jo15765 I am curious as to why you are using Python 2.7 - this is legacy python and should not normally be used by beginners of Python. Official support for 2.x ends on Jan 1, 2020.

Ah - I did not realize my Ubuntu install only had Python 2.7, I'll upgrade and see what happens :)
Reply
#6
(Sep-14-2018, 06:04 PM)jo15765 Wrote: Ah - I did not realize my Ubuntu install only had Python 2.7, I'll upgrade and see what happens :)
Since Ubuntu 16 there Python 3 has been in default install.
Just need to use python3 and install with pip3.
Ubuntu 18.04 and Mint 19 comes with Python 3.6.5 as default version.
Linux Python 3 environment.
Reply
#7
@grantfutuk -> that was exactly the issue! I installed spyder on Ubuntu but I did not realize that it was Spyder2 (running on Python 2.3) - I have upgraded to Spyder3 (running on Python 3.6.6+) and I get the message that I expect!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,255 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Help needed with a "for loop" + error handling tamiri 2 2,471 May-27-2022, 12:21 PM
Last Post: tamiri
  Handling Python Fatal Error richajain1785 7 5,860 Oct-14-2021, 01:34 PM
Last Post: Tails86
  Error Handling JarredAwesome 5 2,899 Oct-17-2020, 12:41 AM
Last Post: JarredAwesome
  Error handling using cmd module leifeng 3 2,849 Jun-06-2020, 06:25 PM
Last Post: leifeng
  Excpetion Handling Getting Error Number gw1500se 4 2,346 May-29-2020, 03:07 PM
Last Post: gw1500se
  Warning / Error handling in python Prarthana_12 1 5,083 Feb-08-2019, 09:21 PM
Last Post: snippsat
  Error Handling/No results from SQL Query JP_ROMANO 7 9,359 Jul-18-2018, 02:31 PM
Last Post: JP_ROMANO
  Error handling parthi1705 0 1,985 Jun-13-2018, 11:57 AM
Last Post: parthi1705
  Error Handling is weird PythonAndArduino 1 2,987 Nov-09-2017, 05:08 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

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