创新互联Python教程:

创新互联是一家成都网站设计、成都网站建设,提供网页设计,网站设计,网站制作,建网站,按需求定制设计,网站开发公司,于2013年成立是互联行业建设者,服务者。以提升客户品牌价值为核心业务,全程参与项目的网站策划设计制作,前端开发,后台程序制作以及后期项目运营并提出专业建议和思路。
编写 Python 程序,通过一个实际例子,利用长度和宽度找到矩形的周长。
这个 Python 程序允许用户输入矩形的长度和宽度。通过使用长度和宽度,这个程序找到一个矩形的周长。计算矩形周长的数学公式:周长= 2 *(长度+宽度)。如果我们知道长度和宽度。
# Python Program to find Perimeter of a Rectangle using length and width
length = float(input('Please Enter the Length of a Triangle: '))
width = float(input('Please Enter the Width of a Triangle: '))
# calculate the perimeter
perimeter = 2 * (length + width)
print("Perimeter of a Rectangle using", length, "and", width, " = ", perimeter)
Please Enter the Length of a Triangle: 35
Please Enter the Width of a Triangle: 88
Perimeter of a Rectangle using 35.0 and 88.0 = 246.0
这个 Python 程序求矩形周长同上。但是,在这个 python 程序中,我们使用 Python 函数来分隔矩形逻辑的周长。
# Python Program to find Perimeter of a Rectangle using length and width
def perimeter_of_Rectangle(length, width):
return 2 * (length + width)
length = float(input('Please Enter the Length of a Triangle: '))
width = float(input('Please Enter the Width of a Triangle: '))
# calculate the perimeter
perimeter = perimeter_of_Rectangle(length, width)
print("Perimeter of a Rectangle using", length, "and", width, " = ", perimeter)