python基礎(chǔ)之循環(huán)語句
發(fā)布日期:2021-12-21 12:28 | 文章來源:源碼中國
循環(huán)語句


多分支


選擇流程 If-else語句

單分支如何使用
# 單分支表示
# if 條件表達式; 比較運算符/邏輯運算符 或者復(fù)合表達
# 代碼指令
# ......
score=60
if score<=60: #滿足條件就會輸出打印提示
print('成績不是太理想')
pass #空語句,結(jié)束跳過,用于填補結(jié)構(gòu)
print('語句運行結(jié)束')

雙分支結(jié)構(gòu)
# 雙分支
# if 條件表達式; 比較運算符/邏輯運算符 或者復(fù)合表達
# 代碼指令
# else:
# 代碼指令
# ......
# 結(jié)果必定會執(zhí)行其中一個分支
if score>60:
print('成績合格')
pass
else:
print('成績不合格')
pass


多分支的使用
# 多分支[多個條件]
# if 條件表達式; 比較運算符/邏輯運算符 或者復(fù)合表達
# 代碼指令
# elif 條件表達式:
# 代碼指令
# elif 條件表達式:
# 代碼指令
# else: ##實際情況可以沒有
# ...... ##特征必會滿足其中一個
# 只要滿足其中一個分支,就會退出本次if語句結(jié)構(gòu)
# 至少存在兩種以上情況可以選擇
# elif之后必須跟上一個條件
# else是一個選配,根據(jù)實際情況來進行選擇
score=int(input('請輸入成績:')) if score>90:
print('您的成績?yōu)閮?yōu)秀')
pass elif score>80:
print('良好')
pass elif score>70:
print('中等')
pass elif score>=60:
print('合格')
pass else:
print('不合格')
pass



# 多分支多條件演練
# 猜拳擊游戲
# 0石頭 1剪刀 2布
import random #導(dǎo)入隨機數(shù)模塊
# 計算機 人
person=int(input('請出拳:[0石頭 1剪刀 2布]'))
computer=random.randint(0,2)
if person==0 and computer==1: #多條件
print('你贏啦....')
pass
elif person==1 and computer==2:
print('你贏啦....')
pass
elif person==2 and computer==0:
print('你贏啦....')
pass
elif person==computer:
print('不錯,平手')
pass
else:
print('輸啦......')
pass
print('程序執(zhí)行完畢')


# if-else 嵌套使用
# 用在一個場景需要分階段或者層次,做出不同的處理
# 要執(zhí)行內(nèi)部的條件 if 語句一定要外部的if語句 滿足條件才可以
xuefen=int(input('請輸入您的學(xué)分:'))
if xuefen>10:
grade = int(input('請輸入您的成績:'))
if grade>=80:
print('您可以升班了')
pass
else:
print('很遺憾,您的成績不達標(biāo)')
pass
pass
else:
print('您的表現(xiàn)也太差了.......')



While 循環(huán)
# 循環(huán)分類 # while 語法結(jié)構(gòu) # while 條件表達式: # 代碼指令 # 語法特點 # 1.循環(huán)必須要有一個初始值 # 2.有條件表達式 # 3.循環(huán)內(nèi)計數(shù)變量必須自增自減,否則會造成死循環(huán) # 循環(huán)使用場景: 循環(huán)次數(shù)不確定,依靠循環(huán)條件來結(jié)束 # 目的:將相似或相同的代碼操作變得更加簡潔,方便重復(fù)使用 # for # while使用 # 輸出1-100之間的數(shù)據(jù) index=1 #定義一個變量 while index<=100: print(index) index+=1 #變量的自增 pass

拳擊游戲循環(huán):
# 多分支多條件演練
# 猜拳擊游戲
# 0石頭 1剪刀 2布
import random #導(dǎo)入隨機數(shù)模塊
# 計算機 人
count=1
while count<=10:
count+=1
person=int(input('請出拳:[0石頭 1剪刀 2布]'))
computer=random.randint(0,2)
if person==0 and computer==1: #多條件
print('你贏啦....')
pass
elif person==1 and computer==2:
print('你贏啦....')
pass
elif person==2 and computer==0:
print('你贏啦....')
pass
elif person==computer:
print('不錯,平手')
pass
else:
print('輸啦......')
pass
print('程序執(zhí)行完畢')

# 打印九九乘法表
row=1
while row<=9:
col=1
while col<=row:
print("%d*%d=%d"%(row,col,row*col))
col+=1
pass
row+=1
pass

# 打印九九乘法表
row=1
while row<=9:
col=1
while col<=row:
print("%d*%d=%d"%(row,col,row*col),end=" ")
col+=1
pass
print()
row+=1
pass

# 打印九九乘法表
row=9
while row>=1:
col=1
while col<=row:
print("%d*%d=%d"%(row,col,row*col),end=" ")
col+=1
pass
print()
row-=1
pass

# 打印直角三角形
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

# 打印等腰三角形
# 打印兩類符號 空格和*
row=1
while row <= 5:
j=1
while j<=5-row: #控制打印空格
print(' ',end='')
j+=1
pass
k=1
while k<=2*row-1:#控制打印*
print('*',end='')
k+=1
pass
print()
row+=1

總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注本站的更多內(nèi)容!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信