寫一個 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!!!")
- If 條件檢查實際成本是否大於銷售金額。如果為真,則 Python 將損失金額打印為實際成本-銷售額
- Elif 語句檢查銷售金額是否大於實際成本。如果為真,它會將利潤額打印為銷售額-實際成本
- 如果上述條件失敗,則意味着無利無損。
用算術運算符計算盈虧的 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!!!
原創文章,作者:JK6ZC,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/130144.html