Python Forum

Full Version: ZeroDivisionError
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey all, I just started coding a few days ago and I'm trying to make a calculator to solve Cramer's rule. I wrote a code that takes values from a user, solves the equation, and works. My problem lies here; There can be a possibility of infinite solutions, and zero solutions. I wrote an 'If' statement saying that if certain variables equal certain variables, to simply print "This system has infinite solutions". The ZeroDivisionError appears after I run the script and input values I know will lead to an infinite solution. I'm not sure why I'm getting this error because I have the If statement located above the regular equation where the zero would be divided. Sorry if this is a paragraph of ramble but I'll include pictures to help. I've tried multiple things like "Except ZeroDivisionError:" and relocating the position of the division. Thanks for your time,
Mitch Big Grin

**Note: Is there like an "If true, End here" kind of command?

[Image: m877jg]
What you need is if...elif...else to make this work easily.

As for the ZeroDivisionError exception, use a try...except block; if a specified error arises during execution of the try block, it will activate the except block to handle the error safely.

try:
    if a1/b1 == a2/b2 and c1/b1 == c2/b2:
        print("...")
    elif a1/b1 == a2/b2 and c1/b1 == c2/b2:
        print("...")
    else:
        Solutionx = beta / charlie
        Solutiony = foxtrot / guelph
        print("Your solution for y is," Solutiony)
        print("Your solution for x is," Solutionx)
except ZeroDivisionError:
    print("Cannot divide by zero.")
Appreciate the help man, thank you !