程式的控制結構

2022-04-27 10:39:08 字數 4463 閱讀 5443

目錄條件判斷及組合

程式的異常處理

"身體質量指數bmi"例項詳解

程式的迴圈結構

根據判斷條件結果而選擇不同向前路徑的執行方式

guess = eval(input("請輸入你猜測的年齡:"))

if guess == 99:

print("猜對了")

​ 請輸入你猜測的年齡:99

​ 猜對了

根據判斷條件結果而選擇不同向前路徑的執行方式

guess = eval(input())

if guess == 99:

print("猜對了")

else:

print("猜錯了")

guess = eval(input())

print("猜{}了".format("對" if guess == 99 else "錯"))

​ 26

​ 猜錯了

score = eval(input())

if score >= 60:

grade = 'd'

elif score >= 70:

grade = 'c'

elif score >= 80:

grade = 'b'

elif score >= 90:

grade = 'a'

print("輸入成績屬於級別{}".format(grade))

操作符

數學符號

描述<

<

小於<=

≤小於等於

>=

≥大於等於

>

>

大於===等於

!=≠不等於用於條件組合的三個保留字

操作符及使用

描述x and y

兩個條件x和y的邏輯與

x or y

兩個條件x和y的邏輯或

not x

條件x的邏輯非

guess = eval(input())

if guess > 99 or guess < 99:

print("猜錯了")

else:

print("猜對了")

try:

num = eval(input("請輸入乙個整數: "))

print(num**2)

# 標註異常型別後,僅響應該異常

# 異常型別名字等同於變數

except nameerror:

print("輸入不是整數")

思路方法

分類國際bmi值(kg/m2)(kg/m2)

國內bmi值(kg/m2)(kg/m2)

偏瘦<18.5

<18.5

正常18.5 ~ 25

18.5 ~ 24

偏胖25 ~ 30

24 ~ 28

肥胖≥30

≥28

# calbmiv1.py

height, weight = eval(input("請輸入身高(公尺)和體重\(公斤)[逗號隔開]: "))

bmi = weight / pow(height, 2)

print("bmi 數值為:".format(bmi))

who = ""

if bmi < 18.5:

who = "偏瘦"

elif 18.5 <= bmi < 25:

who = "正常"

elif 25 <= bmi < 30:

who = "偏胖"

print("bmi 指標為:國際''".format(who))

​ 請輸入身高(公尺)和體重(公斤)[逗號隔開]: 1.8,70

​ bmi 數值為:21.60

​ bmi 指標為:國際'正常'

# calbmiv2.py

height, weight = eval(input("請輸入身高(公尺)和體重\(公斤)[逗號隔開]: "))

bmi = weight / pow(height, 2)

print("bmi 數值為:".format(bmi))

nat = ""

if bmi < 18.5:

nat = "偏瘦"

elif 18.5 <= bmi < 24:

nat = "正常"

elif 25 <= bmi < 38:

nat = "偏胖"

print("bmi 指標為:國內''".format(nat))

​ 請輸入身高(公尺)和體重(公斤)[逗號隔開]: 1.8,70

​ bmi 數值為:21.60

​ bmi 指標為:國內'正常'

# calbmiv3.py

height, weight = eval(input("請輸入身高(公尺)和體重\(公斤)[逗號隔開]: "))

bmi = weight / pow(height, 2)

print("bmi 數值為:".format(bmi))

who, nat = "", ""

if bmi < 18.5:

who, nat = "偏瘦", "偏瘦"

elif 18.5 <= bmi < 24:

who, nat = "正常", "正常"

elif 24 <= bmi < 25:

who, nat = "正常", "偏胖"

elif 25 <= bmi < 28:

who, nat = "偏胖", "偏胖"

elif 28 <= bmi < 30:

who, nat = "偏胖", "肥胖"

else:

who, nat = "肥胖", "肥胖"

print("bmi 指標為:國際'', 國內''".format(who, nat))

​ 請輸入身高(公尺)和體重(公斤)[逗號隔開]: 1.8,70

​ bmi 數值為:21.60

​ bmi 指標為:國際'正常', 國內'正常'

遍歷某個結構形成的迴圈執行方式

for i in range(5):

print('hello:', i)

​ hello: 0

​ hello: 1

​ hello: 2

​ hello: 3

​ hello: 4

for i in range(1, 6, 2):

print('hello:', i)

​ hello: 1

​ hello: 3

​ hello: 5

for c in 'python':

print(c, end=',')

​ p,y,t,h,o,n,

for item in [123, "py", 456]:

print(item, end=" ")

​ 123 py 456

f = open("c:\\users\青檸\desktop\\1.txt") # 開啟檔案所在目錄

f1 = f.read() # 讀取檔案

for i in f1:

print(i,end="") # 列印檔案

# 死迴圈, (ctrl + c 退出執行) 

a = 3

while a > 0:

a = a + 1

print(a)

for
for c in "python":

if c == 't':

continue

print(c, end=',')

for c in "python":

if c == 't':

break

print(c, end=',')

​ p,y,h,o,n,

​ p,y,

while

s = "python"

while s != "":

for c in s:

print(c, end=',')

s = s[:-1]

s = "python"

while s != "":

for c in s:

if c == 't':

break

print(c, end=',')

s = s[:-1]

​ p,y,t,h,o,n,p,y,t,h,o,p,y,t,h,p,y,t,p,y,p,

​ p,y,p,y,p,y,p,y,p,y,p,

程式的控制結構

if true pass else pass a 33 print a if a 99else print b 另一種寫法,只能寫簡單的 and or not 判斷中常用的符號,not a 表示a的否命題try pass 先執行try,如果出現錯誤就跳到except語句中去 except 現實中很多...

程式的控制結構

分支結構是根據判斷條件結果而選擇的不同向前路徑的執行方式,單分支是最簡單的方式 如果 則 猜數 據 guess eval input if guess 99 print 猜對了 根據條件判斷的不同,而選擇不同向前路徑的一種結構 猜數字的例子 guess eval input if guess 99 ...

PYTHON 程式的控制結構

選擇結構 程式流程圖 用簡單的圖形表示問題的解決步驟 起止框,處理匡,判斷框,文件框,流程線,圓形,輸入輸出框 語法 python中大括號不是分割槽作用,是靠語句塊的縮進來體現語句塊術語的範圍 if 條件 縮排語句塊 其餘的語句 if 條件 縮排語句塊 else 縮排語句塊 if語句支援巢狀 多分支...