用一個實際例子編寫 Python 程序,利用長度和寬度找到矩形的面積。
使用長度和寬度查找矩形面積的 Python 程序示例 1
這個 Python 程序允許用戶輸入矩形的長度和寬度。使用這兩個值,它可以找到矩形的面積。如果我們知道矩形的長度和寬度。計算矩形面積的數學公式是:面積=長度*寬度。
# Python Program to find Area 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 area
area = length * width
print("The Area of a Rectangle using", length, "and", width, " = ", area)
使用長度和寬度計算矩形面積的 Python 程序示例 2
這個 Python 代碼查找矩形的面積同上。然而,我們使用函數的概念分離了 python 程序的邏輯。
# Python Program to find Area of a Rectangle using length and width
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
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/248656.html