--- tags: --- FDCSC111 Sep 28th === # 錯誤處理 ## 架構 ```python= try: x = int(input()) print( x ) except Exception: print( "Error" ) ``` # if 判斷式 ## Info **架構** ```python= if 布林值: 要執行的程式碼 ``` - **範例程式碼** ```python= x = int(10000) if x > 1000: x = 1000 ``` 1. 在條件成立後要執行的程式碼要空一格(按一次`tab`) 2. 判斷式可放入: - `bool`布林值 - 條件判斷式 - 數值 ### 關係運算子 |運算子| True|False| | --- | --- | --- | | $>$ | $a \gt b$ | $a \le b$| | $>=$| $a\ge b$| $a \lt b$ | | $<$ | $a \lt b$ | $a \ge b$ | | $<=$ | $a \le b$| $a \gt b$| |$==$ | $a=b$ | $a \ne b$ | |$!=$ | $a \ne b$| $a = b$ |$and$| True|False| | --- | --- | --- | | **True**|True |False| |**False**|False|False| |$or$| True|False| | --- | --- | --- | | **True**|True |True | |**False**|True |False| |$xor$| True|False| | --- | --- | --- | | **True**|False|True | |**False**|True |False| |$not$| True|False| | --- | --- | --- | | 結果 |False|True | ```python= x = int(input()) if x == 0: print("ZERO!!!") # ZERO!!! flag = True if flag: print("TRUE") # TRUE k = int(1) if k: print("not ZERO") # not ZERO ``` ## if ### 流程圖 ```flow s=>start: 程式碼開始執行 e=>end: 結束程式 op2=>operation: 程式碼 cond=>condition: if判斷式 s->cond->op2 cond(no)->e cond(yes)->op2 op2->e ``` ### code ```python= x = int(input()) if x >= 50: print("input is greater than 50") ``` ## else ### 流程圖 ```flow s=>start: 開始程式碼 e=>end: 結束程式碼 op1=>operation: 操作1 op2=>operation: 操作2 cd=>condition: if判斷式 s->cd cd(yes)->op1 cd(no)->op2 op1->e op2->e ``` ### code ```python= x = int(input()) if x <= 50: print("50") else: print(x) ``` ## elif ### 情境 今天就是$William$的老師發考卷的日子,而他的老師會對於不及格的人以及考得好的人特別關照,其中考得好的定義為分數不低於90分,而不及格則是分數低於60分。 $William$身為小老師,自然要替老師完成分類的工作,然而今日$William$早已沉迷於解題中無法自拔,因此他找上你幫忙,為了將工作簡單化,於是你便想運用資研社所學的知識來解決這項問題,現在正是你大展身手的時候。 ### 流程圖 <!-- ```flow s=>start: 程式開始 e=>end: 程式結束 op1=>operation: 輸出"優等" op2=>operation: 輸出"及格" op3=>operation: 輸出"不及格" cond1=>condition: x >= 90 cond2=>condition: x >= 60 s->cond2->op1 cond2(yes)->cond1 cond2(no)->op3 cond1(yes)->op1 cond1(no)->op2 op1->e op2->e op3->e ``` --> ```flow s=>start: 程式開始 e=>end: 程式結束 op1=>operation: 輸出"優等" op2=>operation: 輸出"及格" op3=>operation: 輸出"不及格" cond1=>condition: x >= 90 cond2=>condition: x >= 60 s->cond1->op1 cond1(yes)->op1 cond1(no)->cond2 cond2(yes)->op2 cond2(no)->op3 op1->e op2->e op3->e ``` ```python= x = int(input()) if x >= 90: print("excellent") elif x >= 60: print("pass") else: print("failed") ``` ## 練習題 - [a698. BMI計算](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a698) <!-- :::spoiler 題解code ```python= try: h, w = map(int, input().split()) h, w = input().split() h = int(h) w = int(w) h = h / 100 bmi = w / h / h if bmi > 24: print("BMI超標") elif bmi >= 19: print("標準BMI") else: print("BMI過低") except Exception: print("請符合輸入格式") ``` ::: --> - [a023. 文文的求婚--續集(1行版)](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a023) <!-- Loop === ## for loop ### 簡介 - ```python= for i in (1, 2): print(i) ``` ```python= lst = [1, 2, 3, 4, 5, 9, 8] for i in lst: print(i) ``` ## while loop -->
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up