# Conditionals 條件判斷 > [color=#40f1ef][name=LHB阿好伯, 2020/02/09][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `Python_30` `R & python` [TOC] 🌟 Decision making決策是不管R或是Python或是其他程式語言中都是很重要的功能 決策結構在指定程序中評估或測試的一個或多個條件 以及在條件被確定為**真**時要執行的一個或多個語句 如果條件被認定是**假**的要執行其他語句 下圖為常見決策結構的一般形式  # IF Condition 如果判斷 :::danger ```r= # R 語法 if (條件) { 若為TRUE時執行程式碼 ... } ``` ```python= # Python 語法 if 條件: 若為TRUE時執行程式碼 ``` ::: :::info 在Python使用if等相關函數時須注意縮排 不然就會出現下方的錯誤資訊 IndentationError: unexpected indent (<string>, line 1) 縮排需空四格或是在Rstudio中點擊兩下Tab鍵 ::: 下面簡單的用個範例來判斷a是不是正數 ```r= # R code a <- 3 if(a > 0){ print('A is a positive number') } ``` :::success [1] "A is a positive number" ::: ```python= # Python code a = 3 if a > 0: print('A is a positive number') ``` :::success A is a positive number ::: 若a不是正數時 因為我們沒有告訴R或是Python這種情況 所以不會有任何輸出 ## IF ... ELSE...單層判斷 若是需要將例外或是判斷是FALSE時要執行另一段程式碼在R與Python中都是使用 ==else==  :::danger ```r= # R 語法 if (條件) { 若為TRUE時執行程式碼 ... } else { 若為FALSE時執行程式碼 ... } ``` ```python= # Python 語法 if 條件: 若為TRUE時執行程式碼 else: 若為FALSE時執行程式碼 ``` ::: ```r= # R code a <- -3 if(a > 0){ print('A is a positive number') } else { print('A is a negative number') } ``` :::success [1] "A is a negative number" ::: ```python= # Python code a = -3 if a > 0: print('A is a positive number') else: print('A is a negative number') ``` :::success A is a negative number ::: ## IF...ELIF...ELSE...多層判斷 許多時候我們會遇到需要多個條件的判斷 這時候Python就可以使用 ==elif== 增加判斷 而R則是使用 ==else if==  :::danger ```r= # R 語法 if (條件) { 若為TRUE時執行程式碼 ... } else if (條件) { 若為TRUE時執行程式碼 ... } else { 若為FALSE時執行程式碼 ... } ``` ```python= # Python 語法 if 條件: 若為TRUE時執行程式碼 elif 條件: 若為TRUE時執行程式碼 else: 若為FALSE時執行程式碼 ``` ::: ```r= # R code a <- 0 if(a > 0){ print('A is a positive number') } else if(a < 0){ print('A is a negative number') } else { print('A is zero') } ``` :::success [1] "A is zero" ::: ```python= # Python code a = 0 if a > 0: print('A is a positive number') elif a < 0: print('A is a negative number') else: print('A is zero') ``` :::success A is zero ::: ## 巢狀結構 if函數是可以以嵌套的方式進行構築巢狀結構 在if函數中在插入另一個if函數 > R語言-邁向 Dig Data範例 - 假設1度電費是3.7元 - 為了鼓勵節約能源,如果一個月使用超過100度電費將加收總價的15% - 如果一個月使用小於(含)100度電費可以減免15% - 若家庭有清寒證明同時用電度數小於100度,電費可以再減免30% - 如果電費有小於1元, 以四捨五入處理。  ```r= # R code Electricity <- 100 #用電量 price <- 3.7 #電費 Cold <- TRUE #是否清寒 TRUE or FALSE if(Electricity > 100){ NetPrice <- Electricity * price * 1.15 print(round(NetPrice)) } else if(Electricity <= 100){ if(Cold == TRUE){ NetPrice <- Electricity * price * 0.85 NetPrice <- NetPrice * 0.7 print(round(NetPrice)) }else{ NetPrice <- Electricity * price * 0.85 print(round(NetPrice)) } } ``` :::success [1] 220 ::: ```python= # Python code Electricity = 100 #用電量 price = 3.7 #電費 Cold = "Y" #是否清寒 Y or N if Electricity > 100 : NetPrice = Electricity * price * 1.15 print(round(NetPrice)) elif Electricity <= 100 : if Cold == "Y" : NetPrice = Electricity * price * 0.85 NetPrice = NetPrice * 0.7 print(round(NetPrice)) else : NetPrice = Electricity * price * 0.85 print(round(NetPrice)) else : print(round(NetPrice)) ``` :::success 220 ::: ### 使用 and、or 進行條件判斷-減少巢狀使用 前面的範例中使用了巢狀的結構進行了多個判斷 然而在Python與R語言中可以使用之前在[第三天-運算子](/JcdfGwbBRKKgnyXg6hEmiA)所提到的邏輯運算子進行簡化 |R Operator|Python Operator | Description | | :--------: | :--------: |:--------: | |`&`| and | AND 兩個關係必須為TRUE,則結果為 TRUE | |`|`| or | OR 兩個關係一方為TRUE,則結果為 TRUE| |`!`| not | NOT 得到相反邏輯值,將原本TRUE的結果改成FALSE| ```r= # R code Electricity <- 100 #用電量 price <- 3.7 #電費 Cold <- TRUE #是否清寒 TRUE or FALSE if(Electricity > 100){ NetPrice <- Electricity * price * 1.15 print(round(NetPrice)) } else if(Electricity <= 100 & Cold == TRUE){ #使用&符號進行縮減 NetPrice <- Electricity * price * 0.85 NetPrice <- NetPrice * 0.7 print(round(NetPrice)) }else{ NetPrice <- Electricity * price * 0.85 print(round(NetPrice)) } ``` :::success [1] 220 ::: ```python= # Python code Electricity = 101 #用電量 price = 3.7 #電費 Cold = "Y" #是否清寒 Y or N if Electricity > 100 : NetPrice = Electricity * price * 1.15 print(round(NetPrice)) elif Electricity <= 100 and Cold == "Y" : #使用and進行縮減 NetPrice = Electricity * price * 0.85 NetPrice = NetPrice * 0.7 print(round(NetPrice)) else : NetPrice = Electricity * price * 0.85 print(round(NetPrice)) ``` :::success 220 ::: 這單元非常重要大家可以多找範例練習 全文分享至 https://www.facebook.com/LHB0222/ https://www.instagram.com/ahb0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) # [參考書籍-R語言:邁向Big Data之路](https://www.books.com.tw/exep/assp.php/gtgrthrst4577/products/0010764964?utm_source=gtgrthrst4577&utm_medium=ap-books&utm_content=recommend&utm_campaign=ap-201901) 
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.