Last active 10 months ago

這個 Python 函數計算投資報酬率 (ROI),衡量投資的盈利能力。ROI 公式為 (投資收益 - 投資成本) / 投資成本 * 100,用於評估投資回報的效率,適用於個人理財、企業投資和專案決策。

calculate_roi.py Raw
1def calculate_roi(investment_cost, investment_gain):
2 """
3 計算投資報酬率 (ROI) 的函數。
4
5 :param investment_cost: 投資成本
6 :param investment_gain: 投資收益
7 :return: 投資報酬率 (ROI)
8 """
9 net_profit = investment_gain - investment_cost
10 roi = (net_profit / investment_cost) * 100
11 return roi
12
13# 使用函數計算 ROI
14investment_cost = 100 # 投資成本
15investment_gain = 150 # 投資收益
16
17# 輸出結果
18roi = calculate_roi(investment_cost, investment_gain)
19print(f"投資報酬率 (ROI): {roi}%")
20
21