Python開發【第三篇】:分支循環

1. if 條件語句

  語法:python

if 條件:      
    代碼塊     # 條件爲真執行
else:           # else 可選
    代碼塊     # 條件爲假執行

  示例:函數

n = int(input('請輸入一個數字:'))
if n > 0:
    print('%s 大於 0' % n)
else:
    print('%s 小於 0' % n)

  if 語句支持嵌套:oop

if 條件:
    if 條件:
        代碼塊
    else:
        代碼塊
else:
    代碼塊

  多條件判斷 if - elif - elsecode

  當有多個條件時,老是用 if 判斷,不是那麼方便。爲了偷懶,咱們引入了 elif,即 if - else 的簡寫。對象

score = int(input('請輸入一個分數:'))
if 100 >= score >= 90:
    print('A')
if 90 > score >= 80:
    print('B')
if 80 > score >= 60:
    print('C')
if 60 > score >= 0:
    print('D')
if score < 0 or score > 100:
    print('輸入錯誤')

  用 elif 語句,會簡單方便不少,增長代碼可讀性:input

score = int(input('請輸入一個分數:'))
if 100 >= score >= 90:
    print('A')
elif 90 > score >= 80:
    print('B')
elif 80 > score >= 60:
    print('C')
elif 60 > score >= 0:
    print('D')
else:
    print('輸入錯誤')

2. while 循環語句

  條件爲真,循環體一直執行。it

  語法:io

while 條件:
    循環體

  死循環:class

while True:
    print('死循環')

  示例:登錄

count = 0
while count < 10:       # count 小於 10,一直循環,直至大於 10,退出循環
    print('hello')
    count += 1
print('ok')

  while 循環語句,一樣也能夠擁有 else 自居:

number = 23
running = True

while running:
    guess = int(input('enter a integer: '))

    if guess == number:
        print('congratulations,you guessed it!')
        print('but,you do not win any prizes!')
        running = False       # 循環在此終止,跳出循環,並執行else字句

    elif guess < number:
        print('no,it is a litter higher than that')

    else:
        print('no, it is a litter lower than that')

else:
    print('The while loop is over')
    
print('Done!')

3. for 循環語句

  for 循環語句是另外一種循環語句,對一系列對象進行循環迭代,遍歷序列中的每一個元素。

  語法:

for i in 'she':
    print(i)
s
h
e

  range([start, ] stop [, step=1])函數,能夠用來建立一個整數列表,常與 for 語句搭配。

>>> s = range(5)        # 生成一個 0 - 5 的整數列表
>>> type(s)
<class 'range'>
>>> list(s)
[0, 1, 2, 3, 4]

for i in range(3):
    print(i)
0
1
2

4. break 語句

  break 語句的做用就是終止循環,退出循環。

n = 0
while n < 10:
    n += 1
    if n % 2 == 0:      # 當 n = 2 時退出循環
        break
    print(n)

1

5. continue 語句

  continue 語句用於終止本次循環,再繼續下一次循環,再進行下一次循環以前會判斷循環條件。

n = 0
while n < 10:
    n += 1
    if n % 2 == 0:      # 當 n 爲偶數終止本次循環,繼續下一次循環
        break
    print(n)

1,3,5,7,9

6. 練習題

  1. 利用 while 循環輸出:1 、2 、3 、4 、5 、6 、8 、9 、10

n = 1
while n < 11:
    if n == 7:
        pass
    else:
        print(n)
    n += 1

  2. 計算 1 - 100 的和

n = 1
sum = 0
while n < 101:
    sum += n
    n += 1
print(n)

  3. 計算 1-2+3-4+5-6...99 的和

n = 1
sum = 0
while n < 100:
    temp = n % 2
    if temp == 0:
        sum -= n
    else:
        sum += n
    n += 1
print(sum)

  4. 計算 1 - 100 全部偶數的和

n = 1
while n < 101:
    if n % 2 == 0:
        sum += n
    else:
        pass        # pass 表示該段代碼不執行。
    n += 1
print(sum)

  5. 用戶登陸(三次機會重試)

count = 0
while count < 3:
    user = input('請輸入你的用戶名:')
    psd = input('請輸入你的密碼:')
    if user == 'Alina' and psd == '123456':
        print('歡迎回來 %s' % user)
    else:
        print('輸入錯誤,請重試')
    count += 1
相關文章
相關標籤/搜索