I have a homework assignment to instructs me to play around with functions and exceptions, but one seciton I'm stuck with is a certain condition it tells me to write
o if x is not "So sue me!", but x is still not convertible to an int (by calling int(x)), then raiser(x) raises a ValueError, without any requirements on the exception message.

So i tried the code below:

elif x != 'So sue me!' and x != int(x):
        raise ValueError

Whenever I try to run the function with the statement
raiser('6') raises the ValueError,
raiser (6) allows the statement to pass
but the program is supposed to allow both the statements to pass...Do anyone know what I'm doing wrong? Any help will be apprciated and thanks for your time

try this

elif ((x != 'So sue me!') and (x != int(x))):
        raise ValueError

Mind you that it raises value error because you might be converting non-numeric string (that is, x is not a numeric string like "f"). Posting your whole code will help to pinpoint the problem

the error still comes out the same, but the whole code is listed below and I posted the whole exercise problem just in case.

• E2Exception, an exception class that is a child of Exception.
• E2OddException, an exception class that is a child class (that is, a subclass) of E2Exception.
• raiser, a function that takes one parameter. Here is what raiser(x) does:
o if x == 'So sue me!', then raiser(x) raises an E2Exception with the exception message "New Yorker".
o if x is not "So sue me!", but x is still not convertible to an int (by calling int(x)), then raiser(x) raises a ValueError, without any requirements on the exception message. (This is extremely easy to do.)
o if x converts to an odd int rather than an even one, then raiser(x) raises an E2OddException, without any requirements on the exception message.
o otherwise, raiser(x) does nothing.

class  E2Exception (Exception):
    pass

class  E2OddException (E2Exception):
    pass

def raiser(x):
    if x == 'So sue me!':
        raise E2Exception, "New Yorker"
    elif int(x) % 2 != 0:
        raise E2OddException
    elif ((x != 'So sue me!') and (x != int(x))):
        raise ValueError
    return

if __name__ == '__main__':
    raiser('6')

Thanks for your time

the error still comes out the same

That is not error, it is exactly what you're telling it to do! You tell it to raise value error and it does right? Remember computers aren't intelligent at all, garbage in garbage out!

use this code to see what is going on

class  E2Exception (Exception):
    pass

class  E2OddException (E2Exception):
    pass

def raiser(x):
    if x == 'So sue me!':
        raise E2Exception, "New Yorker"
    elif ((int(x)) % 2) != 0:
        print "Passed 1"
    elif ((x != 'So sue me!') and (x != int(x))):
        print "Passed 2"
    return

if __name__ == '__main__':
    raiser('6')

right, I was raising an error with that certain condition, sorry about the wording earlier.

Just another question though, what if I wanted the '6' from the raiser to be changed to an int so that the print "Passed 2" statement you used earlier would not be printed? would the condition I used earlier

x != int(x)

be correct? or no?

Ideally, I wanted raiser('6') to pass normally without raising any exceptions, just like raiser(6) passes normally for the program.

right, I was raising an error with that certain condition, sorry about the wording earlier.

Just another question though, what if I wanted the '6' from the raiser to be changed to an int so that the print "Passed 2" statement you used earlier would not be printed? would the condition I used earlier

x != int(x)

be correct? or no?

Don't understand what you mean here!

Ideally, I wanted raiser('6') to pass normally without raising any exceptions, just like raiser(6) passes normally for the program.

what are you trying to accomplish in very summary?

right, sorry for the confusion once again

I basically wanted the code to return nothing when I try doing raiser(6) or raiser('6'). If i try doing raiser(5) or raiser('5'), it raises the EOddException, which is the exception I wanted it to raise.

So for this line of code, I was trying to say if x does not equal to so sue me or to any int value (whether presented in string or int initially), it will raise the ValueError. So in that line of code, I'm supposed to convert a string to an int, if it is possible

elif ((x != 'So sue me!') and (x != int(x)))
if x != int(x):

This will tell you whether x is an int or not but should raise a ValueError if x is a non digit string ('a' for example) and will be false is x is a digit string ('6' != 6)

What you want to do is rather something like

if str(x).isdigit():

this will convert x in string (which will stay the same if x is already a string (no exception)) and will look if every character is a digit.

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.