重點:if-else、for、while
練習:畫流程圖,預測執行結果
score = 75
if score >= 60:
print("及格")
else:
print("不及格")
temperature = 30
if temperature > 35:
print("太熱了")
elif temperature < 15:
print("太冷了")
else:
print("天氣剛好")
age = 18
if age >= 18:
print("成年")
else:
print("未成年")
password = "abc123"
if password == "abc123":
print("登入成功")
else:
print("密碼錯誤")
for i in range(3):
print("第", i + 1, "次問候")
for char in "Python":
print(char)
fruits = ["蘋果", "香蕉", "芒果"]
for fruit in fruits:
print("我喜歡", fruit)
for num in range(1, 6):
print(num ** 2)
count = 0
while count < 3:
print("倒數中:", 3 - count)
count += 1
i = 1
while i <= 5:
print(i)
i += 1
password = ""
while password != "1234":
password = input("請輸入密碼: ")
print("密碼正確")
n = 10
while n > 0:
print(n)
n -= 2
for i in range(2):
for j in range(3):
print(f"i={i}, j={j}")
score = 85
if score >= 60:
print("及格")
if score >= 90:
print("成績優異")
for x in range(1, 4):
for y in range(1, 4):
print(x * y, end=" ")
print()
for i in range(1, 6):
if i % 2 == 0:
print(i, "是偶數")
else:
print(i, "是奇數")
for i in range(5):
if i == 3:
break
print(i)
for i in range(5):
if i == 2:
continue
print(i)
i = 0
while i < 5:
if i == 4:
break
print(i)
i += 1
for char in "Python":
if char == "h":
continue
print(char)
x = 10
y = 20
if x < y and y < 30:
print("條件成立")
flag = True
if not flag:
print("旗標為 False")
else:
print("旗標為 True")
score = 88
if score >= 90 or score == 88:
print("成績不錯")
temperature = 22
comfortable = 18 <= temperature <= 28
print("舒適天氣:", comfortable)
for i in range(1, 6):
if i % 2 == 0:
print(f"{i} 是偶數")
else:
print(f"{i} 是奇數")
score = int(input("請輸入分數: "))
if score >= 90:
print("優等")
elif score >= 75:
print("甲等")
elif score >= 60:
print("乙等")
else:
print("丙等")
i = 1
while i <= 5:
print("第", i, "次執行")
i += 1
for x in range(1, 4):
for y in range(1, 4):
print(f"{x} × {y} = {x * y}")