Python 简单计算要求形状面积的实例

yipeiwu_com5年前Python基础

有个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"

以上这篇Python 简单计算要求形状面积的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python3 执行Linux Bash命令的方法

和之前C++执行Linux Bash命令的方法 一样,Python依然支持system调用和popen()函数来执行linux bash命令。 方法一:system调用 #仅仅在一个...

跟老齐学Python之使用Python查询更新数据库

回顾一下已有的战果:(1)连接数据库;(2)建立指针;(3)通过指针插入记录;(4)提交将插入结果保存到数据库。在交互模式中,先温故,再知新。 复制代码 代码如下: >>&g...

python中从for循环延申到推导式的具体使用

本文采用循序渐进的写法,逐步递进. 传统for循环: #获取1到1000000的偶数 #采用传统写法(俗称普通解析) for i in range(1,10**6+1): if(i...

Anaconda下安装mysql-python的包实例

Anaconda下安装mysql-python的包实例

Anaconda下需要使用Python与MySQL数据库进行交互,所以需要import一个mysql-python的包, 但是在ipython中引用的时候发现Anaconda并没有包含该...

详解Python当中的字符串和编码

详解Python当中的字符串和编码

字符编码 我们已经讲过了,字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题。 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理。最早的计算机在设计...