p003_area_of_circle

代码:

1 import math
2 def compute_area_of_circle(r):
3    # return math.pi*r**2
4     #有无空格都行 return (math.pi*r**2)
5      return round(math.pi*r**2,2)
6 print("Area of 2 is: ",compute_area_of_circle(2))
7 print("Area of 3 is: ",compute_area_of_circle(3))
8 print("Area of 10.55 is: ",compute_area_of_circle(10.55))

效果:

 1 Area of 2 is: 12.57

2 Area of 3 is: 28.27

3 Area of 10.55 is: 349.67 

总结:

  1. return后加不加()都行,()前面有无空格都行;
  2. return round(math.pi*r**2,2)可以明确小数点的位数;
  3. 函数体需要缩进。
原文地址:https://www.cnblogs.com/scholarly/p/15418570.html