I wrote this code to calculate the roots of a quadratic function when given the values for a, b, and c in the form ax^2+bx+c=0:
a = input("a")
b = input("b")
c = input("c")
print("Such that ", a, "x^2+", b, "x+", c, "=0,")
def greaterzero(a, b, c):
x = (((b**2 - (4*a*c))**1/2) -b)/2*a
return x
def smallerzero(a, b, c):
x = (-1*((b**2 - (4*a*c))**1/2) -b)/2*a
return x
if smallerzero(a, b, c) == greaterzero(a, b, c):
print("There is only one zero for the quadratic given a, b, and c: ",
greaterzero(a, b, c))
else:
print ("The greater zero for the quadratic is ", greaterzero(a, b, c))
print ("The smaller zero for the quadratic is ", smallerzero(a, b, c))
When I execute the program (in interactive mode) and input 1, 2, and 1 for a, b, and c, respectively, this is the output:
a1
b2
c1
Such that 1 x^2+ 2 x+ 1 =0,
Traceback (most recent call last):
File "jdoodle.py", line 13, in <module>
if smallerzero(a, b, c) == greaterzero(a, b, c):
File "jdoodle.py", line 11, in smallerzero
x = (-1*((b**2 - (4*a*c))**1/2) -b)/2
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
What's the issue here? I didn't formally learn how to use interactive mode yet. I'd like a simple explanation/introduction or a website/tutorial that provides one.