新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:計算收益和損失
創(chuàng)新互聯(lián)Python教程:

創(chuàng)新互聯(lián)是一家專業(yè)提供定西企業(yè)網(wǎng)站建設(shè),專注與做網(wǎng)站、成都網(wǎng)站建設(shè)、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為定西眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進行中。
寫一個 Python 程序,用實例計算盈虧。
使用 Elif 語句計算收益和損失的 Python 程序
這個 python 程序允許用戶輸入產(chǎn)品的銷售額和實際成本。接下來,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 語句檢查銷售金額是否大于實際成本。如果為真,它會將利潤額打印為銷售額-實際成本
- 如果上述條件失敗,則意味著無利無損。
用算術(shù)運算符計算盈虧的 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!!! 網(wǎng)站標題:Python程序:計算收益和損失
轉(zhuǎn)載來于:http://m.fisionsoft.com.cn/article/ccosogh.html


咨詢
建站咨詢
