Dividing by zero error in Python? -


i'm writing program user enters a, b, c, d, e, , f , displays result. if ad - bc = 0, i'm supposed report there no solution. when include part of code where:

if denominator == 0: print("the equation has no solution") 

i keep getting dividing 0 error. numbers use prompt 1.0, 2.0, 2.0, 4.0, 4.0, 5.0 respectively. here's code:

def cramersrule():  = float(input("enter a: ")) b = float(input("enter b: ")) c = float(input("enter c: ")) d = float(input("enter d: ")) e = float(input("enter e: ")) f = float(input("enter f: "))  denominator = ((a * d) - (b * c))  x = (((e * d) - (b * f)) / denominator) y = (((a * f) - (e * c)) / denominator) e = ((a * x) + (b * y))  f = ((c * x) + (d * y)) if denominator == 0:     print("the equation has no solution.") else:     print("x is", x , "and y is" , y) 

please help!

you performing calculations it:

x = (((e * d) - (b * f)) / denominator) y = (((a * f) - (e * c)) / denominator) 

that's why error. must first check if denominator zero.

def cramersrule():      = float(input("enter a: "))     b = float(input("enter b: "))     c = float(input("enter c: "))     d = float(input("enter d: "))     e = float(input("enter e: "))     f = float(input("enter f: "))      denominator = ((a * d) - (b * c))      if denominator == 0:         print("the equation has no solution.")     else:         x = (((e * d) - (b * f)) / denominator)         y = (((a * f) - (e * c)) / denominator)         e = ((a * x) + (b * y))          f = ((c * x) + (d * y))         print("x is", x , "and y is" , y) 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -