Python 实战案例--计算圆、矩形的周长和面积

import math


# 计算圆的面积
def circle_area(radius):
	return math.pi * radius * radius

# 计算圆的周长
def circle_perimeter(radius):
	return math.pi * radius * 2

rad = 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))


# 计算矩形的面积
def rectangular_area(long, wide):
	return long * wide

# 计算矩形的周长
def rectangular_perimeter(long, wide):
	return 2 * (long + wide)


long, wide = eval(input("请分别输入矩形的长和传宽: "))
if long <= 0 and 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)