條件語句的執行過程: if 條件判斷註意:1.每個條件後面要使用冒號 : ,表示條件為True時要執行的代碼;2.使用縮進來劃分代碼塊,相同縮進數的語句在一起組成一個代碼塊。 if...else,單條件判斷 if...elif...else,多條件判斷 ...
條件語句的執行過程:
if 條件判斷註意:
1.每個條件後面要使用冒號 : ,表示條件為True時要執行的代碼;
2.使用縮進來劃分代碼塊,相同縮進數的語句在一起組成一個代碼塊。
if...else,單條件判斷
1 username_store = 'lipandeng'
2 password_store = '123'
3
4 username_input = input('your username:')
5 password_input = input('your password:')
6
7 if username_input == username_store and password_input == password_input:
8 print('welcome {0}'.format(username_input))
9 else:
10 print('Invalid username and password!')
if...elif...else,多條件判斷
1 score = int(input('Enter your score:')) # input()返回的數據類型是str,int()函數是把str轉int。
2
3 if score < 0 or score > 100: # 條件1,此條件為True時執行print(),elif後面的代碼不再執行。
4 print('Scores of the range is 0-100.')
5 elif score >= 90: # 條件2,當條件1為False時判斷條件2,此條件為True時執行print(),elif後面的代碼不再執行。
6 print('Your score is excellent.')
7 elif score >= 60: # 條件3,當條件1和條件2為False時判斷條件3,此條件為True時後執行print(),elif後面的代碼不再執行。
8 print('Your score is good.')
9 else: # 條件4,以上判斷條件都為False時執行的print()。
10 print('Your score is not pass the exam.')