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
favorite, NotImplementedError.
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
provided.
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

I am pretty sure if you don't handle the error, it quits your program.
Try this.

def inputNumber () :
    x = input (’Pick a number: ’)
    if x == 17 :
        raise ValueError, ’17 is a bad number’
    return x

try:
   inputNumber()
except ValueError:
   print 'You picked 17'

Thanks man, I think that worked for what I needed. I just expected the output to match what was in the coursework text

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.