要求:如果不能则抛出异常,显示异常信息" a , b , c 不能构成三角形"; 如果输入的边长中有负数,显示异常信息"三条边不能为负数";
如果可以构成则显示三角形的三个边长。
import numpy as np
def function():
a = float(input('==>'))
b = float(input('==>'))
c = float(input('==>'))
dr = b * b -4 *a *c
r1 = (-b + np.sqrt(dr) ) / 2 *a
r2 = (-b - np.sqrt(dr) ) / 2 *a
if dr > 0 :
print('The roots are %f and %f'%(r1,r2))
elif dr == 0:
print('The root is %f'%r1)
else:
print('The equation has no real roots')
function()
a = input("请输入第1个数字:")
a=int(a) #input()函数,其接收任意任性输入,将所有输入默认为字符串处理,并返回字符串类型,这里需要转为int类型
b = input("请输入第2个数字:")
b=int(b)
c = input("请输入第3个数字:")
c=int(c)
if (a<=0 or b<=0 or c<=0):
print("三条边不能为负数")
elif a+b<c or b+c<a or c+a<b:
print("a , b , c 不能构成三角形")
else:
print("三条边可以构成一个三角形")
print(a,b,c)