Python 實戰案例 計算圓 矩形的周長和面積

2021-10-12 01:18:49 字數 1297 閱讀 6205

import math

# 計算圓的面積

defcircle_area

(radius)

:return math.pi * radius * radius

# 計算圓的周長

defcircle_perimeter

(radius)

:return math.pi * radius *

2rad =

float

(input

("請輸入圓的半徑: "))

if rad <=0:

print

("請輸入,有效的半徑"

)else

: result_1 = circle_area(rad)

result_2 = circle_perimeter(rad)

print

("%s半徑圓的面積是:%s "

%(rad,result_1)

)print

("%s半徑圓的周長是:%s "

%(rad,result_2)

)# 計算矩形的面積

defrectangular_area

(long

, wide)

:return

long

* wide

# 計算矩形的周長

defrectangular_perimeter

(long

, wide)

:return2*

(long

+ wide)

long

, wide =

eval

(input

("請分別輸入矩形的長和傳寬: "))

iflong

<=

0and wide <=0:

print

("請輸入,有效的長和寬"

)else

: result_1 = rectangular_area(

long

, wide)

result_2 = rectangular_perimeter(

long

, wide)

print

("{}長、{}寬的矩行面積是: {}"

.format

(long

, wide, result_1)

)print

("{}長、{}寬的矩行周長是: {}"

.format

(long

, wide, result_2)

)

python藍色的圓 Python 矩形和圓形

exercise 15.1.定義乙個叫做circle 類,類的屬性是圓心 center 和半徑 radius 其中,圓心 center 是乙個 point 類,而半徑 radius 是乙個數字。例項化乙個圓心 center 為 150,100 半徑 radius 為 75 的circle 物件。1 ...

計算幾何模板二(矩形 圓)

計算幾何模板二 矩形 圓 include include include includeusing namespace std define sgn x x 0 1 1 define eps 1e 8 define inf 1e10 define pi acos 1.0 double deg2rad...

Python實戰案例 寫唐詩

pip3 uninstall gensim 這裡如果沒有安裝著不需要解除安裝 pip3 install gensim 3.8.1 from gensim.models import word2vec from random import choice import warnings warnings...