0

Everything in my program works except when it prints out "The solutions are: " s1 and s2 print out with a +0.00j after the answer. How do I format the output to just be two decimal places? As you can see I tried the ,.2f but it didn't work, any help would be appreciated.

import cmath

#converting inputs into floats to avoid ValueError 
a = 0
while a == 0:
    try:
        a = float(input("Enter a value for a: "))
        if a == 0:
           raise ZeroDivisionError
    except ZeroDivisionError:
        print("The value you entered is invalid. Zero is not allowed")
    except ValueError:
            print("The value you entered is invalid. only real numbers")
    else:
            break
print()

while True:
    try:
        b = float(input("Enter a value for b: "))
    except ValueError:
            print("The value you entered is invalid. only real numbers")
    else:
            break
print()

while True:
    try:
        c = float(input("Enter a value for c: "))
    except ValueError:
            print("The value you entered is invalid. only real numbers")
    else:
            break
print()

#Calcualting discriminant, printing it and formatting it    

disc = (b**2) - (4*a*c)
print("The discriminant is equal to: " , disc)
print()

#Calucating/printing solutions if there is one, two, or none
if disc < 0:
    print ("No real solution")
elif disc == 0:
    x = (-b+cmath.sqrt(disc))/(2*a)
    print ("The solution is " , x)
else:
    s1 = (-b+cmath.sqrt(disc))/(2*a)
    s2 = (-b-cmath.sqrt(disc))/(2*a)
    print ("The solutions are " + format(s1,",.2f"), "and " + format(s2, ",.2f"))
Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • 1
    you have a `print ("The solution is " , x)` without the format. Are you referring to this? The square root of 0 is 0j. So you may end up with that – Joe Ferndz Oct 14 '20 at 02:36

2 Answers2

0

Just print out the .real part of your complex numbers:

Change the relevant lines to

print(f"The solution is {x.real:.2f}")

and

print (f"The solutions are {s1.real:.2f} and {s2.real:.2f}")
Selcuk
  • 57,004
  • 12
  • 102
  • 110
-1

I modified the code see if this is slightly better:

# import cmath
import math

#converting inputs into floats to avoid ValueError 
a = 0
while a == 0:
    try:
        a = float(input("Enter a value for a: "))
        if a == 0:
           raise ZeroDivisionError
    except ZeroDivisionError:
        print("The value you entered is invalid. Zero is not allowed")
    except ValueError:
            print("The value you entered is invalid. only real numbers")
    else:
        break
print()

while True:
    try:
        b = float(input("Enter a value for b: "))
    except ValueError:
        print("The value you entered is invalid. only real numbers")
    else:
        break
print()

while True:
    try:
        c = float(input("Enter a value for c: "))
    except ValueError:
        print("The value you entered is invalid. only real numbers")
    else:
        break
print()

#Calcualting discriminant, printing it and formatting it    

disc = (b**2) - (4*a*c)
print("The discriminant is equal to: " , disc)
print()

#Calucating/printing solutions if there is one, two, or none
if disc < 0:
    print ("No real solution")
elif disc == 0:
    # x = (-b+cmath.sqrt(disc))/(2*a)
    x = -b/(2*a)
    print ("The solution is " , x)
else:
    #s1 = (-b+cmath.sqrt(disc))/(2*a)
    #s2 = (-b-cmath.sqrt(disc))/(2*a)
    s1 = (-b+math.sqrt(disc))/(2*a)
    s2 = (-b-math.sqrt(disc))/(2*a)
    print ("The solutions are " + format(s1,",.2f"), "and " + format(s2, ",.2f"))

Result:

Enter a value for a: 1

Enter a value for b: 5

Enter a value for c: 4

The discriminant is equal to:  9.0

The solutions are -1.00 and -4.00
wong.lok.yin
  • 849
  • 1
  • 5
  • 10
  • i downvoted you as the OP may want to use cmath instead of math. The solution should address the printing not the computation part of it. Please fix that so I can upvote. [cmath vs math](https://stackoverflow.com/questions/33684948/difference-between-1-2-math-sqrt-and-cmath-sqrt) – Joe Ferndz Oct 14 '20 at 02:42
  • 1
    @JoeFerndz True, but jason wong has a point here. Using `cmath` is not needed as the code bails when the discriminant is less than 0. – Selcuk Oct 14 '20 at 02:47
  • @jason wong, updating code without explaining the reason does not help OP. Next time please provide clarification on why you chose the option. For me to remove downvote, you need to edit the answer. – Joe Ferndz Oct 14 '20 at 02:54
  • @Joe Ferndz I basically just replace `cmath` by `math`. Using `cmath` or `math` is really depends on the final goal. But you do have a point, I should give some explanation next time. – wong.lok.yin Oct 14 '20 at 03:02