控制流程

Untitled

If statement

Loops

Functions


Untitled

if ( condition ) : #可加括弧可不加
		如果與判斷相符,輸出值
else:
		如果不相符,輸出這裡的值
age = 15
if age < 8:
    print("Movie is free for you!!")
elif (8 <= age < 65):
    print("you need to pay $300!")
else:
    print("You only need to pay $150")
--------------------------------------------------
output: 
you need to pay 300!
if (5 > 3) and []:
    print("the condition is true")
else:
    print("the condition is false")
--------------------------------------------------
output:
the condition is false

Breakfast Program

Using Terminal

Untitled

Untitled

name = input("Enter your name: ")
money = input("Enetr your cash amount: ")
hungry = input("Are you hungry? (Y/N) ")

if hungry == "Y":
    if int(money) >= 30:
        print(f"{name} should go to eat breakfast.")
    else:
        print(f"{name} is hungry but might not have enough money to buy breakfast.")
elif hungry == "N":
    print("You are not hungry")
else:
    print("Please make sure that you enter either Y or N.")
-----------------------------------------------------------
output:
Enter your name: Ray
Enetr your cash amount: 15
Are you hungry? (Y/N) Y
Ray is hungry but might not have enough money to buy breakfast.

Untitled

For Loop

iterable object