Discuss / Python / homework

homework

Topic source

逝去的9211

#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('first bad perand type')
    if not isinstance(b,(int,float)):
        raise TypeError('second bad perand type')
    if not isinstance(c,(int,float)):
        raise TypeError('third bad perand type')
    temp=b*b-4*a*c
    if temp<0:
        return '该方程没有实数解'
    elif a==0:
        return '该方程不是一元二次方程,是一元一次方程'
    elif temp==0:
        return '该方程只有一个解,解为:%f'%(-b/(2*a))
    else: 
        return (-b+math.sqrt(temp))/(2*a),(-b-math.sqrt(temp))/(2*a)

spontaneousor

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

为什么要对a,b,c判断为int和float呢,我觉得只要判断a是否为0(该方程是否为二次方程)即可


  • 1

Reply