Discuss / Python / 交作业

交作业

Topic source

多背单词

#1 Created at ... [Delete] [Delete and Lock User]
import math
def quadratic(a, b, c):

if a==0:
    raise TypeError('Not a quadratic equation')
else:
    Delta=b*b-4*a*c
    if Delta< 0:
        raise TypeError('No solution')
    elif Delta==0:
        x=-b/a*0.5
        return x
    else:
        x_1=(-b+Delta**0.5)/a/2
        x_2=(-b-Delta**0.5)/a/2
        return x_1, x_2

a=float(input('Please input quadratic term coefficient:'))
b=float(input('Please input monomial coefficient:'))
c=float(input('Please constant term:'))
print('The root'r'(s)','of the quadratic equation is',quadratic(a, b, c))

-- coding: utf-8 --

""" Created on Wed Jul 19 08:53:29 2017

@author: Sky """ import math

def my_abs(x): if not isinstance(x, (int, float)): raise TypeError('bad operand Type') if x>= 0: return x else: return -x

def quadratic(a, b, c): d = bb - 4ac if a == 0: if b == 0: return else: return -c / b else: s1 = -b / (2a) s2 = math.sqrt(abs(d)) / (2*a) if d >= 0: return s1-s2, s1+s2 else: t1 = '%.1f-%.1fi' % (s1, s2) t2 = '%.1f+%.1fi' % (s1, s2) return (t1, t2)

print(quadratic(2, 3, 1)) print(quadratic(1, 3, -4)) print(quadratic(1, 0, 1))


  • 1

Reply