Python學(xué)習(xí) day2-2021.2.28(判斷語句與循環(huán)控制)


Python入門課程系列:


簡介

流程:就是計算機執(zhí)行代碼的順序;
流程控制:對計算機代碼執(zhí)行的順序進行有效的管理,只有流程控制才能實現(xiàn)在開發(fā)當(dāng)中的業(yè)務(wù)邏輯

流程控制的分類

  1. 順序流程:就是代碼一種自上而下的執(zhí)行結(jié)構(gòu),也是python默認(rèn)的流程

  2. 選擇流程/分支流程:根據(jù)在某一步的判斷,有選擇的去執(zhí)行相應(yīng)的邏輯的一種結(jié)構(gòu)(if else)

    • 單分支
      if條件句:if 條件表達式:代碼指令

    • 雙分支

      if-else語句:if 條件表達式:代碼指令 else 條件表達式:代碼指令

    • 多分支
      if elif語句

  1. 循環(huán)流程:在 一定的條件下,一直重復(fù)的去執(zhí)行某段代碼的邏輯(while循環(huán)和for循環(huán))

(條件表達式可以是比較運算符/邏輯運算符/復(fù)合的運算符)

??1. if-else語句

if-else

單分支:

#if 條件表達式:比較運算符/邏輯運算符/符合條件的表達式
#   代碼指令
#   ......
 score=60
 if score<=60: #滿足條件會輸出打印提示
     print('成績不是太理想,要繼續(xù)加油哦')
     pass #pass就是隔開代碼用的,是怕復(fù)制代碼時漏了縮進符,導(dǎo)致if條件語句判斷錯誤
 print('語句運行結(jié)束')

雙分支:

 #if 條件表達式:比較運算符/邏輯運算符/符合條件的表達式
 #   代碼指令
 #else:
 #   代碼指令
 score=int(input('請輸入您的成績'))
 if score>60:
     print("及格")
     pass
 else:
     print('不及格,繼續(xù)努力')
     pass #空語句

多分支:

 # if 條件表達式:比較運算符/邏輯運算符/符合條件的表達式
 #   代碼指令
 # elif 條件表達式:
 #   代碼指令
 # ......
 #   else:
 #??特征:
 # 1.只要滿足其中一個分支,就會退出本層if語句結(jié)構(gòu)?!颈囟〞?zhí)行其中一個分支】
 # 2.至少有2種情況可以選擇
 # elif后面必須寫上條件和語句
 # else是選配,根據(jù)實際情況來寫
 score=int(input('請輸入您的成績'))
 if score>=80:
     print("優(yōu)秀")
     pass
 elif score<80 and score >60:
     print('及格')
     pass
 else:
     print('不及格')
     pass
 print('程序運行結(jié)束')

猜拳小游戲:

 import random
 person=int(input('請出拳:[0:石頭 1:剪刀 2:布]'))
 computer=random.randint(0,2)
 if person==0 and computer ==1 or person==1 and computer ==2 or person==2 and computer ==0:
     print('厲害了..你贏了')
     pass
 elif person == computer:
     print('不錯,居然是平手')
     pass
 else:
     print('哈哈,你輸了吧')

#if-else嵌套使用
學(xué)分=int(input("請輸入您的學(xué)分"))
if 學(xué)分>10:
    grade=int(input('請輸入您的成績'))
    if grade>80:
        print('恭喜,你可以升班了')
        pass
    else:
        print('您的成績不達標(biāo)')
        pass
    pass
else:
    print("您的表現(xiàn)不佳")
    pass

??2. while循環(huán)

while:適用于對未知的循環(huán)次數(shù)(用于判斷)
for:適用于已知的循環(huán)次數(shù)(可迭代對象遍歷)

while語法結(jié)構(gòu):while 條件表達式:代碼指令

語法特點??
1. 有初始值
2. 條件表達式
3. 變量【循環(huán)體內(nèi)計數(shù)變量】的自增自減,否則會造成死循環(huán)

使用條件:循環(huán)的次數(shù)不確定,是依靠循環(huán)來結(jié)束
目的:將相似或相同的代碼操作變得更加簡潔,使得代碼可以重復(fù)利用。

#案例1 輸出1-100之間的數(shù)據(jù)
index = 1
while index<=100:
    print(index)
    index+=1 #如果沒有這個循環(huán)體內(nèi)計數(shù)變量,就會陷入死循環(huán),持續(xù)輸出1
    pass

#案例2
#猜拳小游戲 連續(xù)玩10次
import random
variate=1
while variate <= 10:
    person=int(input('請出拳:[0:石頭 1:剪刀 2:布]'))
    computer=random.randint(0,2)
    if person==0 and computer ==1 or person==1 and computer ==2 or person==2 and computer ==0:
        print('厲害了..你贏了')
        pass
    elif person == computer:
        print('不錯,居然是平手')
        pass
    else:
        print('哈哈,你輸了吧')
        pass
    variate+=1
    pass

#案例3 打印99乘法表
row=9
while row>=1:
    col=1
    while col<=row:
        print('%d*%d=%d'%(row,col,row*col),end='') #end通俗理解就是阻止print自帶的換行
        col+=1
        pass
    print()
    row -=1
    pass

#案例4 打印直角三角形
row=1
while row<=7:
    j=1
    while j<=row:
        print('*',end=' ')
        j+=1
        pass
    print()
    row+=1
    pass
#畫倒三角
row=7
while row>=1:
    j=1
    while j<=row:
        print('*',end=' ')
        j+=1
        pass
    print()
    row-=1
    pass

#案例5 打印等腰三角形
row=1
while row<=10:
    j=1
    while j<=10-row: #控制打印空格的數(shù)量
        print(' ',end='') #print“#”更有助于理解。如果end=''的兩個''中間有空格 ,打印的是靠右的直角三角形,
        j+=1
        pass
    k=1
    while k<=2*row-1: #控制打印*號
        print('*',end='')
        k+=1
        pass
    print()
    row+=1

??3. for循環(huán)

for跟while循環(huán)一樣也可以完成循環(huán)(while能做的for也能做)

  • for循環(huán)格式
    for 臨時變量 in 字符串,列表等
    執(zhí)行代碼塊
    執(zhí)行代碼塊
  • for循環(huán)示例
 a = 'python'
 for i in a:
     print(i)
  • for循環(huán) 遍歷列表
li = ['a','b','c','d']
for i in li:
  print(i)
# 語法特點:遍歷操作,依次的取集合容器中的每個值
# for 臨時變量 in 容器:
#       執(zhí)行代碼塊

# 案例1
tages='我是一個中國人'
for item in tages:
    print(item)
    pass

# 案例2 求1到100的加和
# range 此函數(shù)可以生成一個數(shù)據(jù)集合列表 格式:range(起始,結(jié)束,步長),步長不能為0,range取的數(shù)據(jù)是左包含右不包含
sum=0
for data in range(1,101):#左包含右不包含
    sum+=data #求累加和
    #print(data,end=' ')
    pass
print('sum=%d'%sum)
#??注意range的用法:
print(range(0,5))
#range(0, 5)
print(list(range(0,5)))
#[0, 1, 2, 3, 4]

#案例3 打印50-200見所有的奇數(shù)和偶數(shù)
for data in range(50, 201):
    if data % 2 == 0:
        print("%d是偶數(shù)" % data)
        pass
    else:
        print("%d是奇數(shù)" % data)

#案例4 for循環(huán)打印九九乘法表
for i in range(1,10):
    for j in range(1, i+1):
        print('%d*%d=%d'%(i,j,i*j),end='')
        pass
    print() #控制換行
    pass

4. break、continue語句

break:退出(for/while)循環(huán);break 語句可以立即終止當(dāng)前循環(huán)的執(zhí)行,跳出當(dāng)前所在的循環(huán)結(jié)構(gòu)。無論是 while 循環(huán)還是 for 循環(huán),只要執(zhí)行 break 語句,就會直接結(jié)束當(dāng)前正在執(zhí)行的循環(huán)體。
continue:跳過本次循環(huán) ,繼續(xù)下一次循環(huán)(當(dāng)continue條件滿足時 ,本次循環(huán)的語句將不再執(zhí)行,后面的循環(huán)繼續(xù))
這兩個關(guān)鍵字只能用在循環(huán)中

#break案例1 猜拳小游戲,贏三次退出游戲
import random
win = 0
while True:  #while是需要接判斷表達式,true的話就會一直循環(huán)(無限循環(huán)),所以要有break退出。
    if win >= 3:
        print('你已經(jīng)勝利3次')
        break
    else:
        person = int(input('請出拳:[0:石頭 1:剪刀 2:布]'))
        computer = random.randint(0, 2)
        if person == 0 and computer == 1 or person == 1 and computer == 2 or person == 2 and computer == 0:
            print('厲害了..你贏了')
            win +=1
            pass
        elif person == computer:
            print('不錯,居然是平手')
            pass
        else:
            print('哈哈,你輸了吧')
            pass

# break案例2 1到50的加和大于100就退出
sum=0
for item in range(1,51):
    if sum > 100:
        print('循環(huán)執(zhí)行到%d就 退出來了'%item)
        break
        pass
    sum += item
    pass
print("sum = %d"%sum)

#案例3 字符串遍歷
for item in 'I love python':
    if item == 'e':
        break
    print(item)

for item in 'I love python':
    if item == 'o':
        continue
    print(item)

5. 多條件與短路運算

for....else...

案例1
for item in range(1,11):
    print(item,end="")
    if item >= 5:
        break
    pass
else:
    print('已經(jīng)執(zhí)行完了') #只要上面的循環(huán)中出現(xiàn)了break, else的代碼都不會執(zhí)行

案例2 
account = 'hh'
pwd = '123'
for i in range(3):
    zh = input('請輸入賬號:')
    pd = input('請輸入密碼:')
    if account==zh and pwd==pd
        print('恭喜您登錄成功')
        break
        pass
    else:
        print('您的賬號已被系統(tǒng)鎖定')

while...else...

index=1
while index<=10:
    print(index)
    if index==6:
        break
    index+=1
    pass
else:
    print('else執(zhí)行了')
小結(jié)

課后練習(xí):

重復(fù)練習(xí):

  1. 繪制等腰三角形
  2. 編寫剪刀石頭布小游戲,人獲勝次數(shù)超過三次就打印“您已獲勝三次”,并退出程序。否則游戲進行10次,打印“您已累計玩了10次”并退出程序。
  3. 分別使用while循環(huán)和for循環(huán)打印九九乘法表
#第二題
times=0
win=0
while True:
    if win >= 3 and times < 10:
        print('Win 3 times')
        break
    elif times == 10:
        print('Have a break')
        break
    else:
        person = int(input('請輸入:(石頭=0,剪刀=1,布=2)'))
        import random
        computer = random.randint(0, 2)
        if person == 0 and computer == 1 or person == 1 and computer == 2 or person == 2 and computer == 0:
            print('你贏啦')
            win += 1
            pass
        elif person == computer:
            print('平局')
            pass
        else:
            print('你輸啦')
            pass
        times += 1
        pass
    pass

##第三題
col=9
while col >=1:
    row=1
    while row<=col:
        print('%d*%d=%d'%(row,col,row*col),end=' ')
        row+=1
        pass
    print()
    col-=1
    pass
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容