By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

Basic Python Validation

P: 1
Having difficulty creating error handling when inputting an integer

dvdQuant = input("Enter quantity of DVD: ")

how can I get python to return a print "Error Message" if an integer is not entered instead of
Enter quantity of DVD: gyfj
Traceback (most recent call last):
File "I:/Python/CourseworkVer3.py", line 203, in <module>
addDvd(catalogue)
File "I:/Python/CourseworkVer3.py", line 129, in addDvd
dvdQuant = input("Enter quantity of DVD: ")
File "<string>", line 1, in <module>
NameError: name 'gyfj' is not defined

I have the input put into a dictionary so I can multiply with another integer in another function, if I change input to raw_input then when I try and multiply it doesn't recognise the input as an integer

Ihope this isn't too vague and any help would be appreciated
Mar 17 '08 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 849
The quickest way to do this is to use raw_input to get a string, then loop over each character in said string and see if it is between '0' and '9'. If you clear the loop without any issues, you have an integer.
Mar 17 '08 #2

P: 43
Use try except to handle errors.

Expand|Select|Wrap|Line Numbers
  1. try:
  2.     n = int( raw_input("Enter a number: ") )
  3.     print "n=%s, n squared=%s"%(n, n*n)
  4. except ValueError:
  5.     print "Invalid input."
  6.  
Mar 18 '08 #3

Subsciber123
P: 87
Expand|Select|Wrap|Line Numbers
  1. n=raw_input("Number: ")
  2. while not n.isdigit(): # this is a builtin method of all str objects
  3.     print "Error: NaN"
  4.     n=raw_input("Number: ")
  5. n=int(n)
  6.  
I believe that this is the simplest possible way to do it. Looping through the string is tedious and processor intensive, even for short strings. Although this is almost guaranteed to be a short string, you should get into the habit of creating readable and efficient code.
Mar 20 '08 #4

P: 2
Im also new with python, and what would I have to do if I want the script or the section to restart every time the user inputs wrong information??
Mar 21 '08 #5

bvdet
Expert Mod 2.5K+
P: 2,851
Im also new with python, and what would I have to do if I want the script or the section to restart every time the user inputs wrong information??
Following is an example using a while loop.
Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     s = raw_input("Enter an integer")
  3.     try:
  4.         i = int(s)
  5.         break
  6.     except ValueError:
  7.         print 'Invalid input'
  8.  
  9. print 'Your input of %d is an integer.' % i
Mar 21 '08 #6

P: 2
Following is an example using a while loop.
Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     s = raw_input("Enter an integer")
  3.     try:
  4.         i = int(s)
  5.         break
  6.     except ValueError:
  7.         print 'Invalid input'
  8.  
  9. print 'Your input of %d is an integer.' % i
Thanks! I tried to use try-except before without the while loop, and it seems to work very weird, but now with while it works perfect! Thanks!
Mar 21 '08 #7

P: 12
Works great for me 2 :)
Dec 17 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.