HW02 運算子表達式

 1.notebooklm

2.影片


3.學習卡

4.測驗













5.115103127%7+1 使用心得

這段 Python 程式的主要功能是計算使用者提款(Withdraw)的總金額。程式透過提示使用者輸入不同面額鈔票(10Rs、50Rs、100Rs)的張數,計算出加總後的總額並輸出。


逐行程式碼詳細解析

  1. 取得使用者輸入與型態轉換

    N1 = int(input('Enter the number of 10Rs notes u want to withdraw:'))
    N2 = int(input('Enter the number of 50Rs notes u want to withdraw:'))
    N3 = int(input('Enter the number of 100Rs notes u want to withdraw:'))
    
    • input(...):在螢幕顯示提示文字,並讀取使用者輸入的內容(預設為字串型態)。
    • int(...):將讀取到的文字轉換為整數(Integer)型態,以便後續進行數學運算。
    • 變數指派:使用指定運算子(=)將輸入的張數分別儲存至變數 N1(10元張數)、N2(50元張數)與 N3(100元張數)中。
  2. 金額計算運算式

    Total_Amount = N1 * 10 + N2 * 50 + N3 * 100
    
    • 算術運算子(* 與 +):結合乘法運算子(*)與加法運算子(+)構建算術運算式。
    • 運算子優先順序:由於乘法(*)的優先權高於加法(+),電腦會先計算各面額的乘積,最後再進行加總:
      • N1 * 10:10 元鈔票總面額
      • N2 * 50:50 元鈔票總面額
      • N3 * 100:100 元鈔票總面額
    • 計算完畢後,將最終總金額指定給變數 Total_Amount。
  3. 輸出結果

    print(' Total Amount Withdrawn = ', Total_Amount, ' Rs ')
    
    • 將提示字串與計算出之變數 Total_Amount 組合印出至螢幕。

執行範例說明(Output)

當使用者輸入以下數值時:

  • 10 元鈔票張數(N1):5 張 \(\rightarrow 5 \times 10 = 50\)
  • 50 元鈔票張數(N2):5 張 \(\rightarrow 5 \times 50 = 250\)
  • 100 元鈔票張數(N3):4 張 \(\rightarrow 4 \times 100 = 400\)

計算過程:\(50 + 250 + 400 = 700\)
輸出結果:Total Amount Withdrawn = 700 Rs



留言

這個網誌中的熱門文章

HW03 判斷敘述式