Python 计算利润或亏损
在本文中,我们将学习使用Python编写程序来计算利润或亏损。
以下是完成此任务的不同方法:
- 使用if条件语句
-
使用abs()函数
什么是售价、成本价以及利润和亏损
一个消费者支付用于购买产品或商品的价格称为 售价 。它是高于成本价的价格,还包括利润的一部分。
成本价 是卖家购买产品或商品的成本。然后,他加上一部分利润或收益。
从以高于成本价出售物品获得的金额称为 利润 。
Profit = Selling Price – Cost Price.
损失 是以低于成本价出售商品所造成的亏损金额。
Loss = Cost Price - Selling Price
方法1:使用if条件语句
步骤
以下是执行所需任务的算法/步骤:
- 创建一个函数来计算 利润(Profit) ,该函数接受成本价(cp)和售价(sp)作为参数。
-
使用数学公式 sp – cp 来计算利润,并创建一个变量来存储它。
-
使用return关键字返回上述计算得到的利润。
-
创建另一个函数来计算 损失(Loss) ,该函数接受成本价(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()函数计算销售价格和成本价格之间的绝对差。