寫一個 Python 程序,用算術運算符和函數計算一個數的立方,並舉例說明。
尋找數字立方體的 Python 程序
這個 Python 程序允許用戶輸入任何數值。接下來,Python 使用算術運算符找到該數字的立方體。
# Python Program to Calculate Cube of a Number
number = float(input(" Please Enter any numeric Value : "))
cube = number * number * number
print("The Cube of a Given Number {0} = {1}".format(number, cube))
一個數字輸出的 Python 立方體
Please Enter any numeric Value : 5
The Cube of a Given Number 5.0 = 125.0
計算數字立方的 Python 程序示例 2
這個 Python 立方數的例子同上,但是在這裡,我們使用的是指數運算元。
# Python Program to Calculate Cube of a Number
number = float(input(" Please Enter any numeric Value : "))
cube = number ** 3
print("The Cube of a Given Number {0} = {1}".format(number, cube))
Please Enter any numeric Value : 10
The Cube of a Given Number 10.0 = 1000.0
用函數求數字立方體的 Python 程序
在這個 Python 程序的立方體編號代碼片段中,我們定義了一個函數,它可以找到給定數字的立方體。
# Python Program to Calculate Cube of a Number
def cube(num):
return num * num * num
number = float(input(" Please Enter any numeric Value : "))
cub = cube(number)
print("The Cube of a Given Number {0} = {1}".format(number, cub))
原創文章,作者:R7216,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127442.html