1.定义方法
#isdigit()判断是否为数字型,下面通过索引判断’.'的左右两边是否为数字型
import math
def judgeNumber(str):#判断字符串是否是数值类型,如果是返回True,负责返回False
result=True
if('.' in str): #是不是整数类型
s = str.split(".")
if(len(s)==2):
if((s[0].isdigit()==True and s[1].isdigit()==True)==False):
result=False
else:
result=False
elif(str.isdigit()==False):
result=False
return result
2.方法传参最终执行
while True:
a=(input("请输入三角形的第一条边:"))
b=(input("请输入三角形的第二条边:"))
c=(input("请输入三角形第三条边:"))
if(judgeNumber(a) and judgeNumber(b) and judgeNumber(c)):
a1=float(a)
b1=float(b)
c1=float(c)
print("计算三角形面积,请输入数字类型")
if(a1+b1>c1 and a1+c1>b1 and c1+b1>a1):
s=(a1+b1+c1)/2
area=math.sqrt(s*(s-a1)*(s-b1)*(s-c1))
print("三角形的面积是{}".format(area))
break
else:
print("任意两边之和没有大于第三边")
print("数据错误")