xxxxxxxxxx
age = 17
if age < 18:
print("未成年人不能上網")
xxxxxxxxxx
score = input("請輸入成績:")
if score == "100":
print("你真棒!"
if score == "60":
print("還要繼續努力!")
💡 input的內容是字符串,而非數字,所以score要等於"字符串",不能直接數字
== != < > >= <=
xxxxxxxxxx
a = 1
b = 2
print(a == b)
print(a != b)
符合比較就返回True,不符合就返回False
xxxxxxxxxx
if a < b:
print("a小於b")
and 兩個條件都符合才為真
xxxxxxxxxx
if a != b and a < b:
print("條件符合")
or 任何一個條件符合就為真
xxxxxxxxxx
if a == b or a < b:
print("a不大於b")
not 表示相反的結果
xxxxxxxxxx
print(not 3 > 9) # 3 > 9是False,加了not就是真
有三種方式:
二選一:if-else
多選一:if-elif(-elif...)
if-elif(-elif...)-else
xxxxxxxxxx
score = float(input("輸入你的分數:")) #運算到這一行,就要在控制器輸入分數
if score >= 90:
print("優秀!")
elif 60 <= score < 90: # elif不一定要用,有的話就要附上條件
print("做得還算不錯!") # 同上,沒有elif就沒有這一行
else: # else後面不需要添加任何東西
print("你還可以進步!")
# 第一個條件滿足了,就會跳過後面的條件(因此上面的< 90是多餘的)
xxxxxxxxxx
score = float(input("輸入你的分數:")) #運算到這一行,就要在控制器輸入分數
if 100 >= score >= 90:
print("優秀!")
elif 60 <= score < 90: # elif不一定要用,有的話就要附上條件
print("做得還算不錯!") # 同上,沒有elif就沒有這一行
elif 0 <= score < 60:
print("你還可以進步!")
else: # else後面不需要添加任何東西
print("分數無效!")
💡 格式:
xxxxxxxxxx
if 條件1:
事情1
if 條件2:
事件2
else:
不滿足條件做的事件
無論是外層或者是內層,均可以if-else、if-elif、甚至是if-elif-else
xxxxxxxxxx
ticket = True # True代表有車票,False代表沒車票
temp = float(input("請輸入你的體溫:"))
if ticket:
print("可以進站!" , end = "==>")
if 36.3 <= temp <= 37.3:
print("體溫正常,安心上車!")
else:
print("體溫異常,要上醫院直通車!")
else:
print("沒票請出去!")