Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ZeroDivisionError
#1
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]
Reply
#2
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.")
Reply
#3
Appreciate the help man, thank you !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ZeroDivisionError laurawoods 1 167 Apr-11-2024, 03:27 AM
Last Post: deanhystad
  I try to import Vpython and I get a "ZeroDivisionError" Jon2222 16 2,598 Jul-26-2023, 07:37 AM
Last Post: Jon2222
  Overcoming ZeroDivisionError: division by zero Error dgrunwal 8 4,989 Jun-12-2020, 01:52 PM
Last Post: dgrunwal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020