Discuss / Python / 求解 出现多余运算

求解 出现多余运算

Topic source

tyian137

#1 Created at ... [Delete] [Delete and Lock User]

-- coding: utf-8 --

import math

def quadratic(a, b, c): s = bb-4a*c if a == 0 and b != 0: r = -c/b print('result:%.2f' %r) if a == 0 and b ==0: print('there is no answer') elif s < 0: print('there is no answer') elif s == 0: r = -b/2/a print('answer:%.2f' %r) else : r1 = (-b-math.sqrt(s))/2/a r2 = (-b+math.sqrt(s))/2/a print('answer1=%.2f' %r1) print('answer2=%.2f' %r2) quadratic(2, 3, 1) quadratic(1, 3, -4)

运算结果: answer1=-1.00 answer2=-0.50 answer1=-4.00 answer2=1.00 answer1=-1.00 answer2=-0.50 None answer1=-4.00 answer2=1.00 None


  • 1

Reply