导语:
本文主要介绍了关于python怎么计算面积?的相关知识,包括用python计算长方形面积的代码,以及python计算球体表面积和体积这些编程知识,希望对大家有参考作用。
用Python计算面积的知识点:
1.矩形的面积和三角形的面积,因为要从终端接收用户的多个参数输入,使用map(int, raw_input().split())方法
2. 圆面积只想保留小数点后两位,就用到了 %.2f
#!/usr/bin/python
#-*-coding:utf-8 -*-
name = raw_input("Please input your name here : ")
if name == '':
print("we don't like anonymous")
else:
choose = input("Which shape would you like to choose 1:Rectangle, 2:Square, 3:Triangle or 4 Round? :")
if choose == 1:
width, height = map(int, raw_input("Input width and height here, like 5 8 :").split())
print( "Area = %d" % (width * height))
elif choose == 2:
length = input("Input width and height here, like 4:")
print( "Area = %d" % length ** 2)
elif choose == 3:
width, height = map(int, raw_input("Input width and height here, like 5 8 :").split())
print( "Area = %d" % (width * height / 2))
elif choose == 4:
diameter = input("Input diameter here like 3 :")
print ("Area = %.2f" % (3.14 * (diameter / 2) ** 2))
else:
print( "Invalid choose")
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 如何使用python定期获取网页内容10/21
- ♥ python中urlretriev获取远程文件的方法10/06
- ♥ 如何在python中实现字符串连接?09/22
- ♥ python中有冒泡排序吗01/05
- ♥ python遇到无限循环后如何退出和结束?09/10
- ♥ python如何判断一个对象的类型09/04
内容反馈