Hey guys...Need some help with a program for class...here's the assignment
If your program detects an error condition, you can make it raise an exception.
Here is an example that gets input from the user and checks for the value 17.
Assuming that 17 is not valid input for some reason, we raise an exception.
def inputNumber () : x = input (’Pick a number: ’) if x == 17 : raise ValueError, ’17 is a bad number’ return x
The raise statement takes two arguments: the exception type and specific information
about the error. ValueError is one of the exception types Python provides
for a variety of occasions. Other examples include TypeError, KeyError, and my
If the function that called inputNumber handles the error, then the program can
continue; otherwise, Python prints the error message and exits:
>>> inputNumber ()
Pick a number: 17
ValueError: 17 is a bad number
The error message includes the exception type and the additional information you
As an exercise, write a function that uses inputNumber to input a
number from the keyboard and that handles the ValueError exception.
That's all fine and dandy...but when I run the orginal inputNumber program, it errors out like this
>>> inputNumber() Pick a number: 17 Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> inputNumber() File "C:/Users/Tony/Documents/Module 3 SLP/inputnumber exercise", line 4, in inputNumber raise ValueError, "17 is a bad number" ValueError: 17 is a bad number >>>
instead of showing what it should, which is this
>>> inputNumber () Pick a number: 17 ValueError: 17 is a bad number
I had the correct output once...but now it will not output as it should...any ideas why?
Thanks in advance