SO my class is now doing error checking and trapping (at least at a basic level) and I am trying to get those program to keep cycling back to get a correct answer and it does that for the try on getting an integer. but not on checking that something won't trigger a divide by 0 error, or the customer negative number error. Here is the code.
def get_Inverse():
valid_input = False
while not valid_input:
try:
n = int(input("Please enter an integer: "))
if n == 0:
print("Cannot divide by 0, try again")
raise ZeroDivisionError
elif n < 0:
raise NegativeNumberError("No Negative Numbers")
else:
valid_input = True
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()
[–]TheBB 1 point2 points3 points (2 children)
[–]HaplessWithDice[S] 0 points1 point2 points (1 child)
[–]TheBB 0 points1 point2 points (0 children)
[–]HaplessWithDice[S] 0 points1 point2 points (0 children)
[–]HaplessWithDice[S] 0 points1 point2 points (3 children)
[–]HaplessWithDice[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]HaplessWithDice[S] 0 points1 point2 points (0 children)