编写 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)
标签: 手机流量卡