Python條件語句和迴圈語句
·條件語句
筆記:
If 布林值:
print(‘hello,world!’)
當表示式為布林表示式時,Flase None 0 ”” () [] {} 都視為假!
@ if-else 語句
當if語句成立,執行if語句後縮排的程式碼,如果if語句不成立,則執行else語句後縮排的程式碼。
name = input("What is your name?") if name.endswith('jimmy'): #當輸入為Jimmy時,表示式為真,否者為假。 print('hello,{}'.format(name)) else: #當輸入為其他時,if語句為假,才會執行else語句。 print('None')
當輸入為jimmy時,條件語句成立:
What is your name?jimmy hello,jimmy
當輸入為其他值時,條件語句不成立:
What is your name?tom None
@ elif 語句
建立一個成績等級查詢系統,當用戶輸入成績時,可以看到成績對應等級:
score = int(input("請輸入你的成績:")) if score >= 90: print('優秀') if score >= 80: print('良好') if score >= 70: print('一般') if score >= 60: print('及格') if score < 60: print('不及格') 列印結果: 請輸入你的成績:80 良好 一般 及格
執行過程中發現:if語句逐條判斷,當輸入成績80時,滿足前中間3個if語句,程式列印了3個輸出結果,顯然不滿足要求。
接著來修改程式,把if語句替換成elif後:
score = int(input("請輸入你的成績:")) if score >= 90: print('優秀') elif score >= 80: print('良好') elif score >= 70: print('一般') elif score >= 60: print('及格') elif score < 60: print('不及格') 列印結果: 請輸入你的成績:80 良好
再次輸入成績80,第一條語句判斷不成立,接著往下執行,只要有一個elif成立時,就不再判斷後面的elif語句。
@ assert 斷言
正常的分數值在0-100之間,以上程式當輸入大於100或者小於0時程式還能照常執行,下面使用斷言法來限定分數的範圍:
score = int(input("請輸入你的成績:")) assert score <= 100 and score >= 0,'請輸入0-100之間的成績' if score >= 90: print('優秀') elif score >= 80: print('良好') elif score >= 70: print('一般') elif score >= 60: print('及格') elif score < 60: print('不及格')
列印結果:
請輸入你的成績:-1
Traceback (most recent call last):
File "test1.py", line 3, in <module>
assert score <= 100 and score >= 0,'請輸入0-100之間的成績'
AssertionError: 請輸入0-100之間的成績
請輸入你的成績:101
Traceback (most recent call last):
File "test1.py", line 3, in <module>
assert score <= 100 and score >= 0,'請輸入0-100之間的成績'
AssertionError: 請輸入0-100之間的成績
請輸入你的成績:80
良好
·迴圈語句
筆記: while 布林值: 表示式@ while迴圈語句:
要讓以上的查詢系統滿足多次查詢的條件,需要繼續優化程式碼,使用while語句:
while True: score = int(input("請輸入你的成績:")) assert score <= 100 and score >= 0,'請輸入0-100之間的成績' if score >= 90: print('優秀') elif score >= 80: print('良好') elif score >= 70: print('一般') elif score >= 60: print('及格') elif score < 60: print('不及格')
這樣就可以實現多次查詢成績啦,當輸入小於0或者大於100的值時,assert斷言將起作用。
end~
****** 幾米花的Python ****** 部落格主頁:https://www.cnblogs.com/jimmy-share/ 歡迎轉載 ~·條件語句
筆記:
If 布林值:
print(‘hello,world!’)
當表示式為布林表示式時,Flase None 0 ”” () [] {} 都視為假!
@ if-else 語句
當if語句成立,執行if語句後縮排的程式碼,如果if語句不成立,則執行else語句後縮排的程式碼。
name = input("What is your name?") if name.endswith('jimmy'): #當輸入為Jimmy時,表示式為真,否者為假。 print('hello,{}'.format(name)) else: #當輸入為其他時,if語句為假,才會執行else語句。 print('None')
當輸入為jimmy時,條件語句成立:
What is your name?jimmy hello,jimmy
當輸入為其他值時,條件語句不成立:
What is your name?tom None
@ elif 語句
建立一個成績等級查詢系統,當用戶輸入成績時,可以看到成績對應等級:
score = int(input("請輸入你的成績:")) if score >= 90: print('優秀') if score >= 80: print('良好') if score >= 70: print('一般') if score >= 60: print('及格') if score < 60: print('不及格') 列印結果: 請輸入你的成績:80 良好 一般 及格
執行過程中發現:if語句逐條判斷,當輸入成績80時,滿足前中間3個if語句,程式列印了3個輸出結果,顯然不滿足要求。
接著來修改程式,把if語句替換成elif後:
score = int(input("請輸入你的成績:")) if score >= 90: print('優秀') elif score >= 80: print('良好') elif score >= 70: print('一般') elif score >= 60: print('及格') elif score < 60: print('不及格') 列印結果: 請輸入你的成績:80 良好
再次輸入成績80,第一條語句判斷不成立,接著往下執行,只要有一個elif成立時,就不再判斷後面的elif語句。
@ assert 斷言
正常的分數值在0-100之間,以上程式當輸入大於100或者小於0時程式還能照常執行,下面使用斷言法來限定分數的範圍:
score = int(input("請輸入你的成績:")) assert score <= 100 and score >= 0,'請輸入0-100之間的成績' if score >= 90: print('優秀') elif score >= 80: print('良好') elif score >= 70: print('一般') elif score >= 60: print('及格') elif score < 60: print('不及格')
列印結果:
請輸入你的成績:-1
Traceback (most recent call last):
File "test1.py", line 3, in <module>
assert score <= 100 and score >= 0,'請輸入0-100之間的成績'
AssertionError: 請輸入0-100之間的成績
請輸入你的成績:101
Traceback (most recent call last):
File "test1.py", line 3, in <module>
assert score <= 100 and score >= 0,'請輸入0-100之間的成績'
AssertionError: 請輸入0-100之間的成績
請輸入你的成績:80
良好