当前分类:300例题>>正文

Python示例:计算收益和损失

来源:互联网   更新时间:2023年6月17日  

Python 示例

写一个 Python示例,用实例计算盈亏。

使用 Elif 语句计算收益和损失的 Python示例

这个 python 程序允许用户输入产品的销售额和实际成本。接下来,Python 使用 Elif 语句基于这两个值计算损失金额或利润金额。

# Python Program to Calculate Profit or Loss

actual_cost = float(input(" Please Enter the Actual Product Price: "))
sale_amount = float(input(" Please Enter the Sales Amount: "))

if(actual_cost > sale_amount):
    amount = actual_cost - sale_amount
    print("Total Loss Amount = {0}".format(amount))
elif(sale_amount > actual_cost):
    amount = sale_amount - actual_cost
    print("Total Profit = {0}".format(amount))
else:
    print("No Profit No Loss!!!")

Python 收益和损失输出

 Please Enter the Actual Product Price: 1500
 Please Enter the Sales Amount: 1200
Total Loss Amount = 300.0

让我试试不同的价值观

Elif 声明为

if(actual_cost > sale_amount):
    amount = actual_cost - sale_amount
    print("Total Loss Amount = {0}".format(amount))
elif(sale_amount > actual_cost):
    amount = sale_amount - actual_cost
    print("Total Profit = {0}".format(amount))
else:
    print("No Profit No Loss!!!")

用算术运算符计算盈亏的 Python示例

在这个盈亏 python 程序中,我们使用的是一个减运算符。

# Python Program to Calculate Profit or Loss

actual_cost = float(input(" Please Enter the Actual Product Price: "))
sale_amount = float(input(" Please Enter the Sales Amount: "))

if(actual_cost - sale_amount > 0):
    amount = actual_cost - sale_amount
    print("Total Loss Amount = {0}".format(amount))
elif(sale_amount - actual_cost > 0):
    amount = sale_amount - actual_cost
    print("Total Profit = {0}".format(amount))
else:
    print("No Profit No Loss!!!")

在这里,我们尝试了所有的可能性,Python 盈亏程序的输出是

 Please Enter the Actual Product Price: 2200
 Please Enter the Sales Amount: 6500
Total Profit = 4300.0
>>> 
 Please Enter the Actual Product Price: 9800
 Please Enter the Sales Amount: 7200
Total Loss Amount = 2600.0
>>> 
 Please Enter the Actual Product Price: 1495
 Please Enter the Sales Amount: 1495
No Profit No Loss!!!
本文固定链接:https://6yhj.com/leku-p-4273.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: 乐酷