知行编程网知行编程网  2022-10-07 14:00 知行编程网 隐藏边栏  254 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于python怎么计算面积?的相关知识,包括用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")

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享