python

【Python】1.3 Lesson 1.3 Get User Input

內容目錄

  • Input(”請輸入資料”):跳出輸入欄位搜集輸入資料
    • 先顯示「請輸入資料」,跳出輸入欄位,等使用者輸入東西後儲存資料
      • 預設輸入的資料類型是字串(string datatype)
      • 若要搜集數字,要在用float把input包起來,把字串轉成浮點數

Practice

Assume that you work as a financial analyst and you have been tasked to develop a Python application that analyzes securities using the Capital Asset Pricing Model (CAPM).

  • 1. Review the concept of CAPM and write down the formula
  • 2. Write a Python code that will receive beta for a given stock, risk-free rate and expected broad market return from the user and prints out the expected return on the stock. Round your answer to two decimal points.
  • 3. Perform a sanity check by comparing your answer to online CAPM calculators. This calculator could be used: https://goodcalculators.com/capm-calculator/

Hints:

  • A sample code output is as shown below:
    • Please enter the name of the stock: Apple
    • Enter the risk-free rate of return in %: 4
    • Enter the stock beta: 1.25
    • Enter the expected broad market rate of return in % (Ex: S&P500): 5.6
    • Using the CAPM formula, the expected return on Apple stock = 6.0%
  • You might need to change datatypes to perform math operation
stock = input("請輸入股票名稱: ")
Rf=float(input("請輸入無風險利率 Risk_free_rate in %: "))
Beta=float(input("請輸入股票貝它值 bete_of_stock in %: "))
Rm=float(input("請輸入市場報酬 braod_return_market in %: "))
expected_return_CAPM=float(Rf+Beta*(Rm-Rf))


expected_return_CAPM_2_decimal=round(expected_return_CAPM,2)
print("使用CAPM公式計算該股票{}的預期報酬為:{}%".format(stock,expected_return_CAPM_2_decimal))

你好,我是蔡至誠PG,任職於《阿爾發證券投顧》,《我畢業五年,用ETF賺到400萬》作者,《提早五年退休:PG 財經個人財務調配術》講師。

Back to top button