[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
    2會員
    7內容數
    學習程式設計的方法,就是要自己動手下去作了才會真正瞭解。
    留言0
    查看全部
    發表第一個留言支持創作者!
    你可能也想看
    [Python] [微進階]數學運算的反直覺結果有時候在使用數學運算時,會出現一些看似反直覺的結果。 這些錯誤可能是由於我們對於數學規則的誤解或者忽略了一些細節所導致的。 以下是一些 Python 的例子: 除餘數 例如負數求餘數。 假設-15 % 90 狀況下 會下意思覺得結果還是 -15 但輸出的結果是 75。
    avatar
    螃蟹_crab
    2024-04-25
    Python中的函式操作們(上)今天來介紹python的函式 函式在python中是非常重要的一環,因為到了後期,程式會越來越複雜。 而函式可以想成是容易管理的小程式,當我們需要使用時,只需呼叫即可。
    Thumbnail
    avatar
    媗日
    2024-04-25
    ITS Python 認證:入門工程師必備?考試實用性探討初入IT產業的人士在學習Python語言後,IT證照如ITS Python認證是否值得考取?本文以ITS證照特點、實施建議和IT認證考試資訊為主,詳述證照的好處和準備時間。
    Thumbnail
    avatar
    Siao Yun Jiang
    2024-04-23
    Python四大容器大解析古有四大名著,現今Python四大容器🤣 哪四個?list串列,tuple元組,dict字典,set集合。 那這四個怎麼分? 一起來看看吧! (以下有手寫與上機實際測試請付費觀看) 以上我精心整理主要會使用到的功能 當然python功能太多了,肯定不只。 實際操作: 大概就這樣?(
    Thumbnail
    avatar
    媗日
    2024-04-06
    【Python】Mac平台上的Jython和JES安裝指導先來名詞解釋jython跟JES: jython是一種實現了Python語言的Java平台版本的解釋器。它允許開發人員在Java虛擬機(JVM)上運行Python代碼,從而實現了Python語言與Java平台的無縫集成。 JES(Jython Environment for Students)是
    Thumbnail
    avatar
    W. C. Chen
    2024-03-27
    【Python】Python在ETL處理的事先準備 - CSV、Excel、SQLite和SQLAlchemyETL是資料倉儲領域中一個重要的概念,全稱為Extract-Transform-Load,中文可譯為"抽取-轉換-載入"。ETL的作用是將來自不同來源的資料抽取出來,經過清理、轉換、整合等處理後,最終將處理好的資料載入到資料倉儲或其他單一的資料存放區
    Thumbnail
    avatar
    W. C. Chen
    2024-03-25
    【Python超簡單】Python 變數的基本觀念在求學階段,你已經對代數的計算熟到不能再熟,所以變數(variable)對你來說應該不至於太陌生,先來看看以下這個例子:   
    avatar
    伯亞
    2024-03-22
    [Python][微進階]Queue佇列中的資料被多個執行緒並行處理在Python中,queue是一個非常有用的模块。 它提供了多種佇列(queue)實現,用於在多線程環境中安全地交換信息或者數據。 佇列(queue)是一種先進先出(FIFO)的數據結構,允許在佇列的一端插入元素,另一端取出元素。(FIFO 是First In, First Out 的縮寫)
    Thumbnail
    avatar
    螃蟹_crab
    2024-03-22
    [Python][微進階]threading 多執行緒平行處理當你需要在 Python 中執行多個任務,但又不希望它們相互阻塞時,可以使用 threading 模組。 threading 模組允許你在單個程序中創建多個執行緒,這些執行緒可以同時運行,從而實現並行執行多個任務的效果。
    Thumbnail
    avatar
    螃蟹_crab
    2024-03-21
    【Python 知識科普】 關於WSGI與ASGI 使用Python開發後端API的經驗中應該會常常看到WSGI與ASGI這兩個名詞, 兩者的差異究竟是什麼呢? 就讓我們來為您科普一番。 什麼是WSGI 全名為「Web Server Gateway Interface」 Web伺服器閘道介面,主要規範HTTP請求如何與伺服器溝通, 通
    Thumbnail
    avatar
    阿Han
    2024-03-19