Discuss / Python / 交作业2015.7.5

交作业2015.7.5

Topic source

Gussun

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

注意: 1、isinstance每次只能检查一个数

# -*- coding: utf-8 -*-

import math

# 定义求解函数
def quadratic(a,b,c):
    if not isinstance(a,(int,float)):
        raise TypeError('a bad operand type')
    if not isinstance(b,(int,float)):
        raise TypeError('b bad operand type')
    if not isinstance(c,(int,float)):
        raise TypeError('c bad operand type')
    x3=b*b-4*a*c
    if x3>=0: 
        x1=(-b+math.sqrt(x3))/(2*a)
        x2=(-b-math.sqrt(x3))/(2*a)
        return x1,x2    
    else:
        return '该函数没有实数解'

print(quadratic(1,8,2))

好像有限制a不能为0.

张吉吉张

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

可以用 if not isinstance((a,b,c),(int,float)) 一并检查

张吉吉张

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

我错了... 好像不可以

Fuckkinger

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

我就改变了一下 为什么就不行了呢 一直报错 line24 in<module> line 8 ,in quadratic TypeError a bad operand type

``` 在此插入代码

```# -- coding: utf-8 --

import math

定义求解函数

def quadratic(a,b,c): if not isinstance(a,(int,float)): raise TypeError('a bad operand type') if not isinstance(b,(int,float)): raise TypeError('b bad operand type') if not isinstance(c,(int,float)): raise TypeError('c bad operand type') x3=bb-4ac if x3>=0: x1=(-b+math.sqrt(x3))/(2a) x2=(-b-math.sqrt(x3))/(2*a) return x1,x2
else: return 0

a1=input() b1=input() c1=input() print(quadratic(a1,b1,c1))

一元二次方程!!


  • 1

Reply