第六課 條件控制語句
1. 條件判斷語句(if語句)
? 執(zhí)行的流程:
? if語句在執(zhí)行時,會先對條件表達式進行求值判斷,
? 如果為True,則執(zhí)行if后的語句
? 如果為False,則不執(zhí)行
? 語法:if 條件表達式 :
代碼塊
? 代碼塊代碼塊中保存著一組代碼,同一個代碼塊中的代碼,要么都執(zhí)行要么都不執(zhí)行
? 代碼塊以縮進開始,直到代碼恢復到之前的縮進級別時結束
? 代碼塊就是一種為代碼分組的機制
2. input() 函數(shù)
? 該函數(shù)用來獲取用戶的輸入
? input()調用后,程序會立即暫停,等待用戶輸入
? 用戶輸入完內容以后,點擊回車程序才會繼續(xù)向下執(zhí)行
? 用戶輸入完成以后,其所輸入的的內容會以返回值得形式返回
3. if-else語句
? 語法:
if 條件表達式 :
代碼塊
else :
代碼塊
? 執(zhí)行流程:
? if-else語句在執(zhí)行時,先對if后的條件表達式進行求值判斷
? 如果為True,則執(zhí)行if后的代碼塊
? 如果為False,則執(zhí)行else后的代碼塊)
4. if-elif-else 語句
# # int() 可以把其他類型的數(shù)據(jù)強制轉換成int類型
# # input()程序阻塞功能
a = int(input('請輸入1,2或者0:'))
if a == 0:
print(123)
elif a == 1:
print("hehe")
else:
print('輸入數(shù)值錯誤')
# if-elif-else 語句
value = int(input('請輸入你的工資:'))
if value >= 30000:
print('有錢任性')
elif value >= 20000:
print('有錢真好')
elif value >= 10000:
print('哥也月薪上萬了')
elif value >= 5000:
print('工資還說的過去')
elif value >= 2000:
print('能養(yǎng)活自己了')
else:
print("吃便便去吧")
? 語法:
if 條件表達式 :
代碼塊
elif 條件表達式 :
代碼塊
elif 條件表達式 :
代碼塊
........
else :
代碼塊
? 執(zhí)行流程:
? if-elif-else語句在執(zhí)行時,會自上向下依次對條件表達式進行求值判斷,
? 如果表達式的結果為True,則執(zhí)行當前代碼塊,然后語句結束
? 如果表達式的結果為False,則繼續(xù)向下判斷,直到找到True為止
? 如果所有的表達式都是False,則執(zhí)行else后的代碼塊
? 總結: if-elif-else中只會有一個代碼塊會執(zhí)行
5. while語句
? 循環(huán)語句可以使指定的代碼塊重復指定的次數(shù).循環(huán)語句分成兩種,while循環(huán) 和 for循環(huán)
? 語法:
while 條件表達式 :
代碼塊
else:
代碼塊
? 執(zhí)行流程:
? while語句在執(zhí)行時,會先對while語句后面的條件表達式進行求值判斷
? 如果判斷結果為True,則執(zhí)行循環(huán)體的邏輯(代碼塊)
? 循環(huán)體執(zhí)行完畢之后,繼續(xù)對條件表達式進行求值判斷,一次類推
? 直到判斷結果為False
i = 0
while i < 20:
print(i)
print('hello')
i += 1
>>> 0-19
>>>hello .....hello (20)
# 求100 以內所有偶數(shù)的和
# 首先拿到1-100所有的數(shù)
# 拿到偶數(shù)
# 相加
i = 0
r = 0
while i < 100:
i += 2
r += i
print(r)
>>>2550
i = 0
r = 0
while i < 100:
i += 1
if i % 2 == 0:
r += i
print(r)
>>>2550
6. 循環(huán)嵌套
? Python 語言允許在一個循環(huán)體里面嵌入另一個循環(huán)。
? 語法
while 表達式:
while 表達式:
代碼塊
代碼塊
i = 0
while i < 5:
print('*', end=' ')
print('*', end=' ')
print('*', end=' ')
print('*', end=' ')
print('*', end=' ')
print()
i += 1
>>>
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
i = 0
while i < 5:
j = 0
while j < 5:
print('*',end=" ")
j += 1
print()
i += 1
>>>
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
i = 0
while i < 5:
j = 0
while j < i + 1:
print('*',end=" ")
j += 1
print()
i += 1
>>>
*
* *
* * *
* * * *
* * * * *
i = 0
while i < 5:
j = 0
while j < 5 - i:
print('*',end=" ")
j += 1
print()
i += 1
>>>
* * * * *
* * * *
* * *
* *
*
###99乘法表
i = 0
while i < 9:
i += 1
j = 0
while j < i:
j += 1
print(f'{j}*{i} = {j*i}',end=" \t")
print()
>>>
1*1 = 1
1*2 = 2 2*2 = 4
1*3 = 3 2*3 = 6 3*3 = 9
1*4 = 4 2*4 = 8 3*4 = 12 4*4 = 16
1*5 = 5 2*5 = 10 3*5 = 15 4*5 = 20 5*5 = 25
1*6 = 6 2*6 = 12 3*6 = 18 4*6 = 24 5*6 = 30 6*6 = 36
1*7 = 7 2*7 = 14 3*7 = 21 4*7 = 28 5*7 = 35 6*7 = 42 7*7 = 49
1*8 = 8 2*8 = 16 3*8 = 24 4*8 = 32 5*8 = 40 6*8 = 48 7*8 = 56 8*8 = 64
1*9 = 9 2*9 = 18 3*9 = 27 4*9 = 36 5*9 = 45 6*9 = 54 7*9 = 63 8*9 = 72 9*9 = 81
for 變量 in 循環(huán)規(guī)則:
for 變量 in 循環(huán)規(guī)則:
代碼塊
代碼塊
7. break和continue
? break 可以用來立即退出循環(huán)語句,包括else語句
i = 0
while i < 20:
if i == 3:
break
print(i)
i += 1
else:
print('執(zhí)行結束')
>>>
0
1
2
? continue 用來跳過當初循環(huán)
i = 0
while i < 20:
i += 1
if i == 3:
continue
print(i)
else:
print('執(zhí)行結束')
>>>
1
2
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
執(zhí)行結束
8. 作業(yè)
#求1000以內的水仙花數(shù)
i = 1
while i < 10:
j = 0
while j < 10:
k = 0
while k < 10:
if i ** 3 + j ** 3 + k ** 3 == int('%d%d%d'%(i,j,k)):
print('%d%d%d'%(i,j,k))
k += 1
j += 1
i += 1
>>>
153
370
371
407