pythonstringquadraticoperands

Quadratic equation: TypeError: bad operand type for unary -: 'str'


I´m new to Python and have been trying to code the quadratic equation, but i keep running into this error: TypeError: bad operand type for unary -: 'str'

def quad_gleichung():
a = input('a:')
b = input('b:')
c = input('c:')

x1 = int(-b + (b**2 - (4*a*c))**(0.5)) / (2*a)
x2 = int(-b - (b**2 - (4*a*c))**(0.5)) / (2*a)


print('Lösung 1:', x1)
print('Lösung 2:', x2)

quad_gleichung()

Can anyone please help me?

Thanks!


Solution

  • The return type of input is string. It needs to be converted to some number type, float or int, depending on use case.

    Therefore, change the assignments to a, b, c to:

    a = int(input("a: ")) # or float(input("a: "))
    b = int(input("b: ")) # or float(input("b: "))
    c = int(input("c: ")) # or float(input("c: "))