python之if循環
- 2020 年 1 月 20 日
- 筆記
if 條件: if語句塊 else: 語句塊
money = int(input("請輸入你兜里的錢:")) if money > 500: print("吃肉") print("喝酒") print("聊天") else: print("吃麵包") print("老乾媽配饅頭") print("蓋澆飯")
if 條件: if語句塊 elif 條件: 語句塊 …. else: else語句塊 #if…elif…else 有一個成立,就不執行其他.
例:
score = int(input("請輸入你的分數:")) if score >= 90: print("優秀") elif score >= 80: print("良好") elif score >= 70: print("中等") elif score >= 60: print("及格") else: print("不及格")
if嵌套 嵌套的層數不要太多,一般不超過3-5層
例:
print("咣咣咣") gender = input("請輸入你的性別:") if gender == "男": print("隔壁有人在等你") else: # 不是男 age = int(input("請問你的年齡是:")) if age >= 25: print("去隔壁,隔壁有人在等你") else: length = int(input("請問你的身高:")) if length >= 200: print("快去隔壁") else: print("進來吧")