Python 简单计算要求形状面积的实例
作者:numen610 时间:2022-10-19 09:02:33
有个Q友问怎么写个程序能按照要求输入,再输出对应形状的面积?
我大概写了几行,没有考虑输出异常,重点想记录下 int 的接收,如下图
知识点就两个
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"
来源:https://blog.csdn.net/numen610/article/details/78404530
标签:Python,计算,面积
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pycharm 关闭search everywhere的解决操作
2022-04-20 02:46:50
![](https://img.aspxhome.com/file/2023/8/131748_0s.jpg)
anaconda安装后打不开解决方式(亲测有效)
2023-01-21 09:40:15
![](https://img.aspxhome.com/file/2023/7/112417_0s.png)
Vue使用JSEncrypt实现rsa加密及挂载方法
2024-06-05 10:02:30
用DW8制作网页中常用的过度效果
2007-10-16 12:56:00
Form表单及django的form表单的补充
2023-07-21 09:40:04
![](https://img.aspxhome.com/file/2023/4/99114_0s.png)
Go处理json数据方法详解(Marshal,UnMarshal)
2024-02-06 11:47:24
用javascript实现的汉字简繁转换功能
2008-05-04 13:15:00
Laravel框架用户登陆身份验证实现方法详解
2024-04-30 08:47:12
解决python "No module named pip"的问题
2021-02-18 13:32:50
matplotlib绘制动画代码示例
2021-09-09 18:02:15
python在指定位置插入字符的实现
2023-07-04 04:56:56
![](https://img.aspxhome.com/file/2023/5/90405_0s.png)
Python可视化分析全球火山分布
2022-01-17 05:32:10
![](https://img.aspxhome.com/file/2023/7/118537_0s.gif)
一篇文章搞懂Python Unittest测试方法的执行顺序
2023-11-01 09:38:01
![](https://img.aspxhome.com/file/2023/7/86547_0s.png)
MySQL Order By索引优化
2011-01-04 19:56:00
Python实现的简单线性回归算法实例分析
2021-06-05 17:16:12
MySQL GRANT用户授权的实现
2024-01-19 16:56:12
selenium与xpath之获取指定位置的元素的实现
2022-01-14 12:02:35
![](https://img.aspxhome.com/file/2023/8/134528_0s.jpg)
用户体验如何提升阿里巴巴的商业价值
2009-07-23 20:29:00
![](https://img.aspxhome.com/file/UploadPic/20097/23/1-12s.gif)
Windows下在CMD下执行Go出现中文乱码的解决方法
2024-04-25 15:17:27
python引入不同文件夹下的自定义模块方法
2023-04-24 18:37:22
![](https://img.aspxhome.com/file/2023/5/86535_0s.jpg)