Control Flow:程式執行順序


if statement (如果…則…)

  1. 用簡單判斷做決策時使用。例:票價分類,小於等於8歲免費,大於8歲300元
  2. 如果(A)符合,則做(B事),否,則做(C事)。
  3. 判斷裡可以放置內容,並回傳布林值:
    1. Comparison Operator ( ==, ! =, >, <, > =, < =)
    2. Logical Operator (and, or, not)
    3. True or False

Untitled

# age < 8 , ticket free
# age >= 8 and age < 65, ticket 300
# age > 65, ticket 150

age = 28

if age < 8:
    print("Your age is " + str(age) + ", movie is free for you !!")
elif 8 <= age < 65:
    print("Your age is " + str(age) + ", you need to pay $300.")
else:
    print("Your age is " + str(age) + ", you need to pay $150.")

Untitled

“for” loop (依據A,執行B)

  1. 使用在iterable object(循環物件),例:list, tuple, set, string等
  2. 最長搭配range()使用:for i in range(a, b, c)

Untitled

#for variable in interble:
#   do something here

for letter in "Hello World":
    if letter == letter.upper():
        print(letter)

Untitled