473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Basic Python Validation

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
7 16935
Laharl
849 Expert 512MB
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
elcron
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
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
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
2,851 Expert Mod 2GB
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
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
Works great for me 2 :)
Dec 17 '09 #8

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

Similar topics

4
by: Richard Spooner | last post by:
Hey, I'm very new to python and am trying to do the following. I may get the jargon wrong at times but hopefully you can see what I'm trying to do... I have created a threaded class which...
27
by: code_wrong | last post by:
Visual Basic (not dot net) what is the best way to check the User has entered an integer into an InputBox? isNumeric() checks for a numeric value .. but does not notify of numbers with decimal...
2
by: Bob Piton | last post by:
I have just discovered how to do ftp with python, and have a question about using macdef. I can connect to the ftp site, using the .netrc file: machine my.ftpsite.com login myuserid password...
3
by: Kevin Feng | last post by:
I have the following simple html file that is trying to send data to a python script, however, I am getting a weird server error: This is my HTML: <html> <FORM METHOD="POST"...
7
by: fxlogx | last post by:
here we discuss the most basic concepts about python.
11
by: frankie_85 | last post by:
Hi everyone, I just made a simple code which is part of my assignment but I can't figure it out what's wrong with it. (always give me error messages) What the code basically does is: a...
21
by: nateastle | last post by:
I have a simple assignment for school but am unsure where to go. The assignment is to read in a text file, split out the words and say which line each word appears in alphabetical order. I have the...
5
by: Gary | last post by:
Hello, I would like to do several checks on a field "First name". I would like to check for the following: No numbers No spaces Two characters or more
1
by: Chaven | last post by:
Hi Ive done some work on a basic python editor, with code folding, syntax highlighting and the idle shell. It also includes a forms editor to quickly build windows forms applications using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.