计算三角形面积周长

一.代码
import math
a=float(input("请输入三角形的边:"))
b=float(input("请输入三角形的边:"))
c=float(input("请输入三角形的边:"))
print("三角形的三边:",a,b,c)
if(a>0 and b>0 and c>0 and a+b>c and a+c>b and b+c>a):
    l=a+b+c
    h=l/2
    s=math.sqrt(h*(h-a)*(h-b)*(h-c))
    print("三角形的周长=%.2f,面积=%.2f"%(l,s))
else:
    print("无法构成三角形")


二.运行结果

请输入三角形的边:3
请输入三角形的边:4
请输入三角形的边:5
三角形的三边: 3.0 4.0 5.0
三角形的周长=12.00,面积=6.00

三.问题
  (1) print格式控制输出

  (2) 重复执行程序

四.解决

 1. 打印字符串
print ("His name is %s"%("Aviad"))

效果:

2.打印整数

print ("He is %d years old"%(25))

效果:

3.打印浮点数

print ("His height is %f m"%(1.83))

效果:

4.打印浮点数(指定保留小数点位数)

print ("His height is %.2f m"%(1.83))

效果:

5.指定占位符宽度

print ("Name:%10s Age:%8d Height:%8.2f"%("Aviad",25,1.83))

效果:

6.指定占位符宽度(左对齐)

print ("Name:%-10s Age:%-8d Height:%-8.2f"%("Aviad",25,1.83))

效果:

7.指定占位符(只能用0当占位符?)

print ("Name:%-10s Age:%08d Height:%08.2f"%("Aviad",25,1.83))

效果:

8.科学计数法

format(0.0015,'.2e')

效果:


发布了7 篇原创文章 · 获赞 4 · 访问量 4097

猜你喜欢

转载自blog.csdn.net/mo926983/article/details/79312916