2024-06-07|閱讀時間 ‧ 約 28 分鐘

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

    決策:

    決策主要講的就是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
    分享至
    成為作者繼續創作的動力吧!
    © 2024 vocus All rights reserved.