有個(gè)Q友問(wèn)怎么寫個(gè)程序能按照要求輸入,再輸出對(duì)應(yīng)形狀的面積?
我大概寫了幾行,沒(méi)有考慮輸出異常,重點(diǎn)想記錄下 int 的接收,如下圖
知識(shí)點(diǎn)就兩個(gè)
1, 長(zhǎng)方形面積 & 三角形面積,因?yàn)橐獜慕K端接收用戶的多個(gè)參數(shù)輸入,所以用了map(int, raw_input().split()) 這種方式
2. 圓面積只想保留小數(shù)點(diǎn)后兩位,就用到了 %.2f 來(lái)接收

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 簡(jiǎn)單計(jì)算要求形狀面積的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/numen610/article/details/78404530