def test(test_name): def trilateral(): count = 0 while True: a = input('请输入三角形的第一条边长,请输入整数或小数') if a.isdigit(): global A A = float(a) break else: count+=1 if count==3: print(f'第一条边连续错误{count+1}次,系统认为在恶意操作,结束了整个程序的执行') exit() while True: b = input('请输入三角形的第二条边长,请输入整数或小数') if b.isdigit(): global B B = float(b) break else: print(f'输入内容格式不正确,请重新输入第二条边的边长,累计错误次数{count+1}次') count += 1 if count == 4: print(f'已错误输入{count+1}次,累计4次跳过本次循环') break while True: c = input('请输入三角形的第三条边长') if c.isdigit(): global C C = float(c) break else: count += 1 print(f'输入内容格式不正确,请重新输入第三条边的边长,累次错误次数{count+1}次') if count ==6: print(f'已错误输入{count+1}次,累计6次跳过本次循环') break return test_name() return trilateral @test #装饰器对输入的三条边进行校验 def PascalsTriangle(): try: if A+B>C and A+C>B and B+C>A: #判断是否为三角形,是则输出三角形面积和周长 perimeter = A +B +C dimension = (perimeter*(perimeter-A)*(perimeter-B)*(perimeter-C))*0.5 print(f'这个三角形的周长是:{perimeter}\n这个三角形的面积是:{dimension}') else: print('你输入的这三个数,不能构成一个三角形,请重新输入') PascalsTriangle() #函数内部调用自己,再次执行前,先执行装饰器中的代码 except Exception as e: print(e) PascalsTriangle()
用学过的python写一个计算三角形面积和周长
于 2022-08-23 20:18:42 首次发布