编写一个 Python 程序,使用长度和宽度以及实际示例来查找矩形的周长。
此 Python 程序允许用户输入矩形的长度和宽度。通过使用长度和宽度,此程序可以找到矩形的周长。计算矩形周长的数学公式是:周长 = 2 * (长度 + 宽度)。如果我们知道长度和宽度。
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
使用长度和宽度查找矩形周长的程序示例 2
此 程序 查找矩形的周长与上面相同。但是,在此 Python 程序 中,我们使用 Python 函数 将矩形周长的逻辑分开。
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)
