首页 > 编程知识 正文

Python 程序:利用长度和宽度计算矩形周长

时间:2023-11-25 09:12:54 阅读:309014 作者:JJUV

编写 Python 程序,通过一个实际例子,利用长度和宽度找到矩形的周长。

使用长度和宽度查找矩形周长的 Python 程序示例 1

这个 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 程序示例 2

这个 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)

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。