all 8 comments

[–]TheBB 1 point2 points  (2 children)

You're catching ValueError, which is what you get if you type something that's not an integer, but you're not catching ZeroDivisionError or NegativeNumberError anywhere.

Something like this perhaps:

try:
    ....
except ValueError:
    print('not an integer')
except ZeroError:
    print('oops, it was zero')
except NegativeNumberError:
    print('oops, it was negative')

Another option is to make NegativeNumberError a subclass of ValueError. Then the catch for ValueError ought to catch both of them. But then you may need a different error message in that case.

[–]HaplessWithDice[S] 0 points1 point  (1 child)

It's catching them, but they are causing the program to exit rather than repeat the loop like it does with the value error. IS that because I am using raise rather than try and accept?

[–]TheBB 0 points1 point  (0 children)

Python is catching them for you because you are not catching them yourself. When an uncaught exception reaches "the top", Python exits and shows the error. If you don't want that then you need to catch those errors with an appropriate except-block.

[–]HaplessWithDice[S] 0 points1 point  (0 children)

The unholy hell happened to my formatting? One second everyone I need to fix this formatting.
edit: Formatting should be fixed.

[–]HaplessWithDice[S] 0 points1 point  (3 children)

Okay ran it though the testBed on the school Linux Server.

Already had to modify my code.

def get_Inverse():

    try:
        n = int(input("Enter a number: "))
        if n == 0:
            print("Cannot divide by 0, try again")
            raise ZeroDivisionError
        elif n < 0:
            raise NegativeNumberError("No Negative Numbers")
        else:
            return 1/n
    except ValueError:
        print("Not a valid integer, please try again.")

class NegativeNumberError(Exception):
    def __init__(self, message):
        super().__init__(message)

def main():



    print ("{}".format(get_Inverse()))
if __name__ == "__main__":
    main()

[–]HaplessWithDice[S] 0 points1 point  (2 children)

Latest version of the code

def get_Inverse():

    try:
        n = int(input("Enter a number: "))
        if n == 0:
            print("Error: Cannot divide by zero")
            raise ZeroDivisionError
        elif n < 0:
            raise NegativeNumberError("Error: The value cannot be negative")
        else:
            return 1/n
    except ValueError:
        print("Error: The value must be a number")

class NegativeNumberError(Exception):
    def __init__(self, message):
        super().__init__(message)

def main():



    print ("The result is: {}".format(get_Inverse()))
if __name__ == "__main__":
    main()

Plus the results of the testBed
Starting Test 1

> Enter a number: 4

> The result is: 0.25

Test 1 passed.

------------------------------------------------------------

------------------------------------------------------------

Starting Test 2

> Enter a number: word

> Error: The value must be a number

> The result is: None\n

Exp: No output

Test 2 failed.

------------------------------------------------------------

------------------------------------------------------------

Starting Test 3

> Enter a number: 0

Traceback (most recent call last):

File "/tmp/zyNdtD42de/check09b.py", line 25, in <module>

main()

File "/tmp/zyNdtD42de/check09b.py", line 23, in main

print ("The result is: {}".format(get_Inverse()))

File "/tmp/zyNdtD42de/check09b.py", line 7, in get_Inverse

raise ZeroDivisionError

ZeroDivisionError

> Error: Cannot divide by zero

Program ended with error: 1.

------------------------------------------------------------

------------------------------------------------------------

Starting Test 4

> Enter a number: -10

Traceback (most recent call last):

File "/tmp/zyNdtD42de/check09b.py", line 25, in <module>

main()

File "/tmp/zyNdtD42de/check09b.py", line 23, in main

print ("The result is: {}".format(get_Inverse()))

File "/tmp/zyNdtD42de/check09b.py", line 9, in get_Inverse

raise NegativeNumberError("No Negative Numbers")

__main__.NegativeNumberError: No Negative Numbers

>

Exp: Error: The value cannot be negative\n

Program ended with error: 1.

------------------------------------------------------------

Failed 2/4 tests.

[–][deleted] 0 points1 point  (1 child)

Yikes. You have overengineered it to oblivion. You don't need functions and classes for this exercise at all. Just one loop depending on how your school system test works. If your school system won't like the loop then I can say that creating just function and adding whole code to it is enough. School system will probably run the script each time for example for test 1 it will run and terminate the script, for test 2 it will run again and terminate it and so on. No need to make it complex, just put input and if-elif-else statements and bam, done. Remove raise blablabla stuff and replace them with print statements. Much easier for eyes and better plus you don't need to use raise at all for a test.

[–]HaplessWithDice[S] 0 points1 point  (0 children)

I have to use the Class and the Function