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
7 16935
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.
Use try except to handle errors. -
try:
-
n = int( raw_input("Enter a number: ") )
-
print "n=%s, n squared=%s"%(n, n*n)
-
except ValueError:
-
print "Invalid input."
-
- 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.
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??
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. - 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
Following is an example using a while loop. - 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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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"...
|
by: fxlogx |
last post by:
here we discuss the most basic concepts about python.
|
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...
|
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...
|
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
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |