443,818 Members | 1,262 Online
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 addDvd(catalogue) File "I:/Python/CourseworkVer3.py", line 129, in addDvd dvdQuant = input("Enter quantity of DVD: ") File "", line 1, in 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
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 try:     n = int( raw_input("Enter a number: ") )     print "n=%s, n squared=%s"%(n, n*n) except ValueError:     print "Invalid input."   Mar 18 '08 #3

 P: 87 Expand|Select|Wrap|Line Numbers n=raw_input("Number: ") while not n.isdigit(): # this is a builtin method of all str objects     print "Error: NaN"     n=raw_input("Number: ") n=int(n)   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

 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 while True:     s = raw_input("Enter an integer")     try:         i = int(s)         break     except ValueError:         print 'Invalid input'   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 while True:     s = raw_input("Enter an integer")     try:         i = int(s)         break     except ValueError:         print 'Invalid input'   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