写一个 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 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!!!
标签: 乐酷