import math
def quadratic(a, b, c):
a = int(a)
b = int(b)
c = int(c)
if ( (b**2 - 4 * a * c) < 0 ):
print('no outcome!')
pass
elif ( (b**2 - 4 * a * c) == 0 ):
print('only one outcome')
outcome = -b / 2*a
print(outcome)
pass
elif ( (b**2 - 4 * a * c) > 0 ):
outcome1 = ( -b + math.sqrt(b**2 - 4*a*c) )/2*a
outcome2 = ( -b - math.sqrt(b**2 - 4*a*c) )/2*a
print('here are two outcomes')
print('%.1f , %.1f' % (outcome1,outcome2))
pass
def isnub(s):
try:
nb = float(s)
return True
except ValueError as e:
return False
while 1:
a = input('a\n')
b = input('b\n')
c = input('c\n')
if not ( isnub(a) and isnub(b) and isnub(c) ):
print('input digit please')
continue
else:
quadratic(a, b, c)
PYTHON:写一个求二次方程的根的程序
最新推荐文章于 2025-08-30 15:37:45 发布
本文介绍了一个使用Python实现的二次方程求解算法。该算法能够根据输入的二次方程系数a、b和c,判断方程的根的情况,并计算出实数解。文章详细展示了如何处理不同判别式的三种情况:无实数解、一个实数解和两个实数解。
1万+

被折叠的 条评论
为什么被折叠?



