def quadratic(a, b, c):
import math
delta = b*b - 4*a*c
if a == 0:
x=(-c)/b
return x
elif delta == 0:
x=(-b)/(2*a)
return x
elif delta > 0:
x1 = (-b+math.sqrt(delta))/(2*a)
x2 = (-b-math.sqrt(delta))/(2*a)
return x1, x2
else:
return None
测试
# Test
print('------------------------------------------------')
print('Let\'s get the root of this equation: "ax^2 + bx + c = 0"')
a = int(input('Input number for \'a\':'))
b = int(input('Input number for \'b\':'))
c = int(input('Input number for \'c\':'))
print('------------------------------------------------')
quadraticTest = quadratic(a, b, c)
if (quadraticTest != None):
#print('The equation\'s root:\n => %s, %s' % (quadraticTest[0], quadraticTest[1]))
print('The equation\'s root:')
print(quadraticTest)
else:
print('No solution.')
整个文件内容
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# function: ax^2 + bx + c = 0
# Formula:
# delta = b^2 - 4*a*c
# if deltar >= 0:
# x1 = (-b + math.sqrt(delta))/(2*a)
# x2 = (-b - math.sqrt(delta))/(2*a)
# description:If you give me 3 arg,I'll give you my result by sqrt!
def quadratic(a, b, c):
import math
delta = b*b - 4*a*c
if a == 0:
x=(-c)/b
return x
elif delta == 0:
x=(-b)/(2*a)
return x
elif delta > 0:
x1 = (-b+math.sqrt(delta))/(2*a)
x2 = (-b-math.sqrt(delta))/(2*a)
return x1, x2
else:
return None
# Test
print('------------------------------------------------')
print('Let\'s get the root of this equation: "ax^2 + bx + c = 0"')
a = int(input('Input number for \'a\':'))
b = int(input('Input number for \'b\':'))
c = int(input('Input number for \'c\':'))
print('------------------------------------------------')
quadraticTest = quadratic(a, b, c)
if (quadraticTest != None):
#print('The equation\'s root:\n => %s, %s' % (quadraticTest[0], quadraticTest[1]))
print('The equation\'s root:')
print(quadraticTest)
else:
print('No solution.')
回风哥哥
quadratic方法
测试
整个文件内容