使用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