[Python][ITS]#2 程式控制流程: 決策及迴圈

更新 發佈閱讀 7 分鐘

決策:

決策主要講的就是if...else這種流程控制的程式,假如某條件成立就執行某些動作,否則就執行別的動作,大致上就是這個概念。

需要特別注意的地方 縮排

1. 在條件成立後所要執行的動作每一行的縮排要一致(就是要排整齊的意思),不然會被當作是其它動作而沒被跟著一起執行或程式結構混亂而造成編譯上的錯誤。

例子:

num = int(input("please enter your number ? "))
guest_position = ""

if num > 10 :
print("please go to the left side")
guest_position = "Left"
else :
print("please go to the right side")
guest_position = "Right"
print("Now you are in ", guest_position, " side") # 這一行沒有跟上面對齊縮排,所以不算是else條件成立後要執行的動作
  1. 巢狀決策還是要注意縮排,如此才能知道某個動作是在什麼條件下成立會執行的。

例子:

id_num = int(input("please enter your ID ?"))
floor = ""
if id_num > 500:
print("please go to the 2nd floor")
floor = "2nd"
else :
if id_num > 100:
print("please go to the 3rd floor")
floor = "3rd"
else :
print("please go to the 4th floor")
floor = "4th"
print("Now you are in the ",floor," floor")
  1. 多重決策的使用,一樣要注意縮排。

例子:

id_num = int(input("please enter your ID ?"))
floor = ""
if id_num > 500:
print("please go to the 2nd floor")
floor = "2nd"
elif id_num > 100 :
print("please go to the 3rd floor")
floor = "3rd"
else :
print("please go to the 4th floor")
floor = "4th"
print("Now you are in the ",floor," floor")
  1. if, else, elif 等這幾個關鍵字該行最後一定要加上 ":" ,這是很容易忽略的點。

迴圈:

使用上主要可以分成for loop和 while loop這兩者,而兩者都可以搭配break和continue這兩個關鍵字來調整迴圈的執行的流程,可以讓迴圈的設計更有彈性。

跟for loop常常一起使用的就是range( )這個函式,(參考官網說明: link)。一般使用上很容易搞不清楚總共是多少個值?最大到多少? 所以,請記得"結束值永遠不會出現在生成的序列中"這句話,就可以了。(例: range(10) 指的就是 0,1,2,3,4,5,6,7,8,9 這幾個數字,因為沒有指定開始值,所以,預設是從0開始,沒有指定step值,預設就是1)

需特別注意for ... else以及 while ... else的用法,以及在搭配break和continue時,要不要執行else的動作。

當for loop與while loop有正常執行完,才會再執行else後面的動作。

注意: 這邊的正常執行完成不包括break,因為break就代表要跳出整for ... else 或while ... else的範圍。所以,就不會再執行else的動作了。

例子:

num = int(input("please enter a number for the whole loop ? "))
b_num = int(input("please enter a number you want to break ?"))
c_num = int(input("please enter a number you want to skip ?"))

for i in range(num):
if i == c_num:
continue
elif i == b_num:
break
else:
print("this is the ",i,"th floor")
else:
print("you are now in the ",i,"th floor")

執行結果 1:

please enter a number for the whole loop ? 10
please enter a number you want to break ?12
please enter a number you want to skip ?5
this is the 0 th floor
this is the 1 th floor
this is the 2 th floor
this is the 3 th floor
this is the 4 th floor
this is the 6 th floor
this is the 7 th floor
this is the 8 th floor
this is the 9 th floor
you are now in the 9 th floor


​執行結果 2:

please enter a number for the whole loop ? 10
please enter a number you want to break ?5
please enter a number you want to skip ?8
this is the 0 th floor
this is the 1 th floor
this is the 2 th floor
this is the 3 th floor
this is the 4 th floor
留言
avatar-img
留言分享你的想法!
avatar-img
Joec's Maker
2會員
7內容數
學習程式設計的方法,就是要自己動手下去作了才會真正瞭解。
Joec's Maker的其他內容
2024/06/13
2024/06/13
2024/06/06
2024/06/06
2024/06/06
ITS python認證內容含蓋六大主題
2024/06/06
ITS python認證內容含蓋六大主題
看更多
你可能也想看
Thumbnail
還在煩惱平凡日常該如何增添一點小驚喜嗎?全家便利商店這次聯手超萌的馬來貘,推出黑白配色的馬來貘雪糕,不僅外觀吸睛,層次豐富的雙層口味更是讓人一口接一口!本文將帶你探索馬來貘雪糕的多種創意吃法,從簡單的豆漿燕麥碗、藍莓果昔,到大人系的奇亞籽布丁下午茶,讓可愛的馬來貘陪你度過每一餐,增添生活中的小確幸!
Thumbnail
還在煩惱平凡日常該如何增添一點小驚喜嗎?全家便利商店這次聯手超萌的馬來貘,推出黑白配色的馬來貘雪糕,不僅外觀吸睛,層次豐富的雙層口味更是讓人一口接一口!本文將帶你探索馬來貘雪糕的多種創意吃法,從簡單的豆漿燕麥碗、藍莓果昔,到大人系的奇亞籽布丁下午茶,讓可愛的馬來貘陪你度過每一餐,增添生活中的小確幸!
Thumbnail
在這一章中,我們介紹了PHP中的流程控制語句,包括if、else if、else、三元運算子、switch語句、for迴圈、foreach迴圈、while迴圈、do-while迴圈、循環嵌套以及控制迴圈的語句如break、continue、goto和return。
Thumbnail
在這一章中,我們介紹了PHP中的流程控制語句,包括if、else if、else、三元運算子、switch語句、for迴圈、foreach迴圈、while迴圈、do-while迴圈、循環嵌套以及控制迴圈的語句如break、continue、goto和return。
Thumbnail
此章節的目的是介紹Java程式語言中的流程控制結構,包括條件語句(if, else if, else)、三元運算子、switch語句,以及各種迴圈(for, foreach, while)。同時,也解釋了如何在迴圈中使用控制語句來改變程式的執行流程。每種主題都配有示例程式碼以幫助理解。
Thumbnail
此章節的目的是介紹Java程式語言中的流程控制結構,包括條件語句(if, else if, else)、三元運算子、switch語句,以及各種迴圈(for, foreach, while)。同時,也解釋了如何在迴圈中使用控制語句來改變程式的執行流程。每種主題都配有示例程式碼以幫助理解。
Thumbnail
本章節帶領讀者了解 Kotlin 的流程控制語法,包括條件判斷、當做三元運算子使用的 if 表達式、用來替代 switch 語句的 when 語句、用於迴圈運作的 for 和 while 語句,以及控制迴圈執行流程的語句和標籤概念。該章節目的在於讓讀者深入掌握流程控制功能,進一步提升編程技巧。
Thumbnail
本章節帶領讀者了解 Kotlin 的流程控制語法,包括條件判斷、當做三元運算子使用的 if 表達式、用來替代 switch 語句的 when 語句、用於迴圈運作的 for 和 while 語句,以及控制迴圈執行流程的語句和標籤概念。該章節目的在於讓讀者深入掌握流程控制功能,進一步提升編程技巧。
Thumbnail
本篇介紹了Swift程式語言中的各種流程控制元素,包括條件語句(如if, else if, else),三元運算子,多條件分支判斷的switch語句,以及各種迴圈(如for迴圈,while迴圈,以及repeat-while迴圈)。同時也詳細解釋了如何進行迴圈嵌套,以及如何使用控制迴圈語句。
Thumbnail
本篇介紹了Swift程式語言中的各種流程控制元素,包括條件語句(如if, else if, else),三元運算子,多條件分支判斷的switch語句,以及各種迴圈(如for迴圈,while迴圈,以及repeat-while迴圈)。同時也詳細解釋了如何進行迴圈嵌套,以及如何使用控制迴圈語句。
Thumbnail
本章節提供了關於Typescript中流程控制元素的詳細介紹,包括if, else if, else語句,三元運算子,switch語句,各種for迴圈,while迴圈,循環嵌套和控制迴圈語句(break,continue和標籤)的使用。
Thumbnail
本章節提供了關於Typescript中流程控制元素的詳細介紹,包括if, else if, else語句,三元運算子,switch語句,各種for迴圈,while迴圈,循環嵌套和控制迴圈語句(break,continue和標籤)的使用。
Thumbnail
本章節主要介紹了JavaScript中的流程控制,包括條件語句(如if、else if、else和三元運算子)和循環結構(如for迴圈、while迴圈等)。同時,也提供了如何使用break、continue和label來控制迴圈的執行。
Thumbnail
本章節主要介紹了JavaScript中的流程控制,包括條件語句(如if、else if、else和三元運算子)和循環結構(如for迴圈、while迴圈等)。同時,也提供了如何使用break、continue和label來控制迴圈的執行。
Thumbnail
本文是C#入門教程的一部分,涵蓋了流程控制的各種語句與迴圈。這包括if、else if和else語句,三元運算子,switch語句,以及for、foreach和while迴圈。文中還介紹了如何在迴圈中使用break、continue、return和goto語句。
Thumbnail
本文是C#入門教程的一部分,涵蓋了流程控制的各種語句與迴圈。這包括if、else if和else語句,三元運算子,switch語句,以及for、foreach和while迴圈。文中還介紹了如何在迴圈中使用break、continue、return和goto語句。
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News