目錄
語法(掌握)
if判斷是幹什麼的呢?if判斷實際上是在模擬人作判斷。就是說若是這樣幹什麼,若是那樣幹什麼。對於ATM系統而言,則須要判斷你的帳號密碼的正確性。python
if
學什麼都是爲了讓計算機向人同樣工做,咱們無時無刻都在判斷。路邊路過一個生物,你會判斷兩我的是否是會表白?首先會判斷這個生物是否是人類,而且這我的類是個女人,年齡大於18小於20幾歲。你首先須要記錄一堆數據,而後纔會用你的大腦去判斷。if表示if成立代碼成立會幹什麼。nginx
if 條件:
代碼1
代碼2
代碼3
...
# 代碼塊(同一縮進級別的代碼,例如代碼一、代碼2和代碼3是相同縮進的代碼,這三個代碼組合在一塊兒就是一個代碼塊,相同縮進的代碼會自上而下的運行)
# if
cls = 'human'
gender = 'female'
age = 18
if cls == 'human' and gender == 'female' and age > 16 and age < 22:
print('開始表白')
print('end...')
開始表白
end...
if...else
if 條件:
代碼1
代碼2
代碼3
...
else:
代碼1
代碼2
代碼3
...
if...else表示if成立代碼成立會幹什麼,else不成立會幹什麼。markdown
# if...else
cls = 'human'
gender = 'female'
age = 38
if cls == 'human' and gender == 'female' and age > 16 and age < 22:
print('開始表白')
else:
print('阿姨好')
阿姨好
if...elif...else
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...不然幹什麼。post
# if...elif...else
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('阿姨好')
考慮下
練習(掌握)
練習1:成績評判
- 若是 成績>=90,打印"優秀"
- 若是 成績>=80 而且 成績<90,打印"良好"
- 若是 成績>=70 而且 成績<80,打印"普通"
- 其餘狀況:打印"差"
# 成績評判
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
良好
練習2:模擬登陸註冊
# 模擬登陸註冊
user_from_db = 'tank'
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: tank
password: 123
username or password error
if的嵌套(掌握)
若是咱們表白的時候,表白成功的時候咱們是否是會作什麼,表白不成功是否是又會會作什麼呢?ui
# 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('阿姨好')
開始表白 我逗你玩呢
掌握—》熟悉—》瞭解
- 掌握:滾瓜爛熟。
- 熟悉:正背如流。
- 瞭解:看到可以想起。lua