-2

How do I make the code below to accept both float and int as input?

'''This program helps you solve quadratic equation using

the quadratic formula principle

Quadratic formula, x = -b +- sqrt(b^2 - 4ac)/2a

'''

from math import sqrt

try:
    a = int(input('Input the value of a: '))
    b = int(input('Input the value of b: '))
    c = int(input('Input the value of c: '))
    numeratorA = (-b) + sqrt((b*b)-(4*a*c))
    numeratorB = (-b) - sqrt((b*b)-(4*a*c))
    denominator = 2 * a
    root1 = numeratorA/denominator
    root2 = numeratorB/denominator
    print('The roots are:', root1, "and", root2)

except:
    print('Enter only numbers!')?
tripleee
  • 175,061
  • 34
  • 275
  • 318

1 Answers1

0

input() by default reads in terminal inputs as strings. You need to typecast them to the data type you need. And you can just cast all numerical inputs as floats that'll just make integers like 3 to 3.00 which won't be an issue in your case.

E.g. b = float(input("some text")) You can do your computations afterwards without any worries.