Discuss / Python / 作业

作业

Topic source

庞通同学

#1 Created at ... [Delete] [Delete and Lock User]
   # -*- coding: utf-8 -*-

import math

def quadratic(a, b, c): 
    if not isinstance(a, (int,float)):
        raise TypeError('bad operand type')
    if not isinstance(b, (int,float)):
        raise TypeError('bad operand type')
    if not isinstance(c, (int,float)):
        raise TypeError('bad operand type')
    d=b*b-4*a*c
    x1=0
    x2=0
    if d>=0:
        x1=(-b+math.sqrt(d))/(2*a)
        x2=(-b-math.sqrt(d))/(2*a)
        return x1,x2
    else:
        return '这个方程没有实数解'
# 测试:
print(quadratic(2,3,1))
print(quadratic(1,3,-4))

  • 1

Reply