4,Python程式結構

2021-10-04 06:37:44 字數 1678 閱讀 2629

a =

3#單分支選擇結構

if a >3:

print

("this number is greater than 3"

)#雙分支選擇結構

if a >3:

print

("this number is greater than 3"

)else

:print

("this number is smaller or equal to 3"

)#多分支選擇結構

if a >3:

print

("this number is greater than 3"

)elif a <3:

print

("this number is smaller than 3"

)else

:print

("this number is equal to 3"

)#選擇結構巢狀

if a >5:

if a <10:

print

("this number is greater than 5 but smaller than 10"

)else

:print

("this number is greater than or equal to 10"

)else

:print

("this number is smaller than or equal to 5"

)

i =

1while i <10:

print

(i) i +=

1

#列印1-9的值

for i in

range(9

):print i

break語句在迴圈中被用於直接結束迴圈,可以與if語句配合使用。

for i in

range(9

):a = i*

3if a =24:

break

else

print a

continue語句在迴圈中使用可以跳過本次迴圈,並直接進入下一次迴圈。

#列印1-11間的所有偶數

for i in

range(2

,11):

if i%2==

0:print i

else

:continue

salary =

for i in

range(9

):a =

input

("please input the salary of the 9 employees!"

)print

("input q or q to quit"

)if a <0:

continue

if a.upper = q:

break

float

(a))

else

:print

("you've finished inputing all of the salarys!"

)

python 程式結構

多路分支 if語句其他 2 迴圈語句 for else語句 break,contineu,pass while迴圈 3 函式 4 返回值 5 函式文件 input gender input 請輸入性別 print 你輸入的性別是 format gender 表示這裡是新增的字串,0表示第乙個字串 s...

Python程式結構

if 條件表示式 語句1語句2 語句3.if語句1 字串的真假 只有空字串為false,其餘全為true a 字串非空 if a print 輸出為true else print 輸出為false 執行結果 輸出為true if語句2 age 19if age 16 print 去網咖 else p...

Python程式結構

條件語句 if 判斷 滿足判斷條件執行的 塊 else 不滿足判斷條件執行的 塊 if 判斷1 滿足判斷條件1執行的 塊 elif 判斷2 不滿足判斷1,滿足判斷2執行的 塊 else 不滿足所有判斷條件執行的 塊 條件語句可以巢狀 while 判斷 滿足判斷條件執行的迴圈體 continue 跳過...