使用Python計算利潤或虧損
在這篇文章中,我們將學習一個Python程式來計算利潤或虧損。
以下是完成此任務的不同方法:
使用If條件語句
使用abs()函式。
什麼是售價、成本價以及利潤和虧損?
消費者購買產品或商品的價格稱為售價。它高於成本價,也包括一部分利潤。
成本價是賣方購買產品或商品的成本。之後,他會加上一部分收益或利潤。
以高於成本價的價格出售商品所獲得的金額稱為利潤。
Profit = Selling Price – Cost Price.
虧損是以低於成本價的價格出售商品而造成的損失。
Loss = Cost Price - Selling Price
方法1:使用If條件語句
演算法(步驟)
以下是執行所需任務應遵循的演算法/步驟:
建立一個計算利潤的函式,該函式接受成本價 (cp) 和售價 (sp) 作為引數。
使用數學公式sp - cp計算利潤,並建立一個變數來儲存它。
使用return關鍵字返回上述計算的利潤。
建立另一個計算虧損的函式,該函式接受成本價 (cp) 和售價 (sp) 作為引數。
使用數學公式cp - sp計算虧損,並建立一個變數來儲存它。
使用return關鍵字返回上述計算的虧損。
建立兩個單獨的變數來儲存輸入的成本價和售價。
使用if條件語句和'=='運算子來檢查輸入的售價是否等於成本價。
如果條件為真,則列印既無利潤也無虧損。
使用elif條件語句檢查售價是否大於成本價。
如果條件為真,則透過將成本價和售價作為引數傳遞給calculateProfit函式來獲取利潤值,從而列印利潤。
否則,透過將成本價和售價作為引數傳遞給calculateLoss函式來獲取虧損值,從而列印虧損。
示例
以下程式使用if條件語句計算利潤或虧損:
# creating a function to calculate Profit that
# accepts the cost price(cp) and selling price(sp) as arguments
def calculateProfit(cp, sp):
# formula for calculating profit
resultProfit = (sp - cp)
# returning the resulting profit
return resultProfit
# creating a function to calculate Loss that
# accepts the cost price(cp) and selling price(sp) as arguments
def calculateLoss(cp, sp):
# formula for calculating loss
resultLoss = (cp - sp)
# returning the resultant loss.
return resultLoss
# input cost price
cost_price = 500
# input selling price
selling_price = 1000
# checking whether the selling price is equal to the cost price
if selling_price == cost_price:
# printing Neither profit nor loss if the condition is true
print("Neither profit nor Loss")
# checking whether the selling price is greater than the cost price
elif selling_price > cost_price:
# Calling calculateProfit function by passing cost price and selling price
# as arguments and printing profit if the condition is true
print("The Profit is", calculateProfit(cost_price, selling_price))
else:
# Else calling calculateLoss function by passing cost price and
# selling price as arguments and printing Loss
print("The Loss is", calculateLoss(cost_price, selling_price))
輸出
執行上述程式後,將生成以下輸出:
The Profit is 500
方法2:使用abs()函式
演算法(步驟)
以下是執行所需任務應遵循的演算法/步驟:
建立一個函式來計算售價和成本價之間的差值,該函式接受成本價 (cp) 和售價 (sp) 作為引數。
使用abs()函式計算售價和成本價之間的差值。
返回售價和成本價之間的差值。
建立兩個單獨的變數來儲存輸入的成本價和售價。
像以前的方法一樣列印利潤/虧損。
示例
以下程式使用abs()函式計算利潤或虧損:
# creating a function to calculate the difference between sp and cp that
# accepts the cost price(cp) and selling price(sp) as arguments
def calculateDifference(cp, sp):
# calculating the difference between the selling price and the cost price
difference = abs(sp - cp)
# returning the absolute difference of selling and cost price
return difference
# input cost price
cost_price = 500
# input selling price
selling_price = 1000
# checking whether the selling price is equal to the cost price
if selling_price == cost_price:
# printing Neither profit nor Loss if the condition is true
print("Neither profit nor Loss")
# checking whether the selling price is greater than the cost price
elif selling_price > cost_price:
# printing profit if the condition is true, by calling calculateDifference
# function by passing cost price and selling price as arguments
print("The Profit is", calculateDifference(cost_price,selling_price))
# Else this is the case of loss
else:
# Else printing Loss if the condition is true by calling calculateDifference
# function by passing cost price and selling price as arguments to it
print("The Loss is", calculateDifference(cost_price,selling_price))
輸出
執行上述程式後,將生成以下輸出:
The Loss is 200
結論
在這篇文章中,我們學習了什麼是售價和成本價,以及如何在給定售價和成本價的情況下建立一個Python程式來確定利潤或虧損。作為使用兩個不同函式的替代方法,我們學習瞭如何使用abs()函式計算售價和成本價之間的絕對差值。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP