编写 Python 程序,使用长度和宽度以及实际示例查找矩形面积。
Python 程序,使用长度和宽度查找矩形面积 示例 1
此 Python 程序允许用户输入矩形的长度和宽度。使用这两个值,它找到矩形的面积。如果我们知道矩形的长度和宽度。计算矩形面积的数学公式是:面积 = 长度 * 宽度。
length = float(input('Please Enter the Length of a Triangle: '))
width = float(input('Please Enter the Width of a Triangle: '))
# calculate the area
area = length * width
print("The Area of a Rectangle using", length, "and", width, " = ", area)

Python 程序,使用长度和宽度计算矩形面积 示例 2
此 Python 计算面积的代码与上面相同。但是,我们使用带两个参数的 python 程序的概念分离了 函数 逻辑,它会返回一个值。
def area_of_Rectangle(length, width):
return length * width
length = float(input('Please Enter the Length of a Triangle: '))
width = float(input('Please Enter the Width of a Triangle: '))
# calculate the area of a Rectangle
area = area_of_Rectangle(length, width)
print("The Area of a Rectangle using", length, "and", width, " = ", area)
Please Enter the Length of a Triangle: 125
Please Enter the Width of a Triangle: 65
The Area of a Rectangle using 125.0 and 65.0 = 8125.0