有個Q友問怎么寫個程序能按照要求輸入,再輸出對應形狀的面積?
我大概寫了幾行,沒有考慮輸出異常,重點想記錄下 int 的接收,如下圖
知識點就兩個
1, 長方形面積 & 三角形面積,因為要從終端接收用戶的多個參數輸入,所以用了map(int, raw_input().split()) 這種方式
2. 圓面積只想保留小數點后兩位,就用到了 %.2f 來接收

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/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 簡單計算要求形狀面積的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/numen610/article/details/78404530