if判斷是幹什麼的呢?if判斷實際上是在模擬人作判斷。就是說若是這樣幹什麼,若是那樣幹什麼。對於ATM系統而言,則須要判斷你的帳號密碼的正確性。code
學什麼都是爲了讓計算機向人同樣工做,咱們無時無刻都在判斷。路邊路過一個生物,你會判斷兩我的是否是會表白?首先會判斷這個生物是否是人類,而且這我的類是個女人,年齡大於18小於20幾歲。你首先須要記錄一堆數據,而後纔會用你的大腦去判斷。if表示if成立代碼成立會幹什麼。input
if 條件: 代碼1 代碼2 代碼3 ... # 代碼塊(同一縮進級別的代碼,例如代碼一、代碼2和代碼3是相同縮進的代碼,這三個代碼組合在一塊兒就是一個代碼塊,相同縮進的代碼會自上而下的運行)
cls = 'human' gender = 'female' age = 18 if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('開始表白') print('end...')
開始表白 end...
if 條件: 代碼1 代碼2 代碼3 ... else: 代碼1 代碼2 代碼3 ...
if...else表示if成立代碼成立會幹什麼,else不成立會幹什麼。it
cls = 'human' gender = 'female' age = 38 if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('開始表白') else: print('阿姨好')
阿姨好
if 條件1: 代碼1 代碼2 代碼3 ... elif 條件2: 代碼1 代碼2 代碼3 ... elif 條件3: 代碼1 代碼2 代碼3 ... ... else: 代碼1 代碼2 代碼3 ...
if...elif...else表示if條件1成立幹什麼,elif條件2成立幹什麼,elif條件3成立幹什麼,elif...不然幹什麼。class
cls = 'human' gender = 'female' age = 28 if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('開始表白') elif cls == 'human' and gender == 'female' and age > 22 and age < 30: print('考慮下') else: print('阿姨好')
考慮下
若是咱們表白的時候,表白成功的時候咱們是否是會作什麼,表白不成功是否是又會會作什麼呢?登錄
# if的嵌套 cls = 'human' gender = 'female' age = 18 is_success = False if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('開始表白') if is_success: print('那咱們一塊兒走吧...') else: print('我逗你玩呢') else: print('阿姨好')
開始表白 我逗你玩呢
# 成績評判 score = input("your score: ") score = int(score) if score >= 90: print('優秀') # elif score >= 80 and score < 90: elif score >= 80: print('良好') # elif score >= 70 and score < 80: elif score >= 70: print('普通') else: print('差')
your score: 80 良好
# 模擬登陸註冊 user_from_db = 'nick' pwd_from_db = 123 user_from_inp = input('username: ') user_from_inp = input('password: ') if user_from_inp == user_from_db and pwd_from_inp == pwd_from_db: print('login successful') else: print('username or password error')
username: nick password: 123 username or password error