import math
def main():
print("Let us finds the solutions to a quadratic\n")
a, b, c = eval(input("Do enter the coefficients (a, b, c):"))
delta = b * b - 4 * a * c
if a == 0:
x = -b / c
print("\nThere is an solution",x)
elif delta < 0:
print("\nThe equation has no real roots!")
elif delta == 0:
x = -b / (2 * a)
print("\nThere is a double root at",x)
else:
discRoot = math.sqrt(delta)
x1 = (-b + discRoot) / (2 * a)
x2 = (-b - discRoot) / (2 * a)
print("\nThe solutions are:", x1, x2)
main()
错误代码
Do enter the coefficients (a, b, c):4 4 1
Traceback (most recent call last):
File "D:/c/mooc/04/01.quadratic4.py", line 20, in
main()
File "D:/c/mooc/04/01.quadratic4.py", line 4, in main
a, b, c = eval(input("Do enter the coefficients (a, b, c):"))
File "", line 1
4 4 1
^
SyntaxError: invalid syntax