編寫 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)
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/309847.html