Python循環(huán)語句

2018-07-24 16:44 更新

1.循環(huán)介紹

1.1 什么是循環(huán)?

什么是循環(huán)呢,循環(huán)就是能夠讓程序不斷的重復(fù)執(zhí)行的一種方法。循環(huán)可以分為計數(shù)循環(huán)與條件循環(huán):

  • 計數(shù)循環(huán):按照次數(shù)進行循環(huán),當(dāng)?shù)竭_(dá)指定次數(shù)時候,程序停止或者執(zhí)行其他。
  • 條件循環(huán):按照條件進行循環(huán),當(dāng)滿足某一個條件時候,程序停止或者執(zhí)行其他。

1.2 為什么要循環(huán)?

我們知道計算機最大的作用就是通過計算讓很多東西自動化;所謂程序就是給計算機規(guī)劃一條路徑,并讓她按照路徑進行計算。但是這個還不能算是自動化,畢竟只計算了一次,當(dāng)我們需要程序不斷的算,那么就需要循環(huán)。

  • 現(xiàn)在我們知道,循環(huán)就是讓程序不斷重復(fù)執(zhí)行.

2.計數(shù)循環(huán)

2.1 range()的基本用法:

for 變量名 in range(0,5):
    print(變量名)

0
1
2
3
4

這段就是循環(huán)的基本用法,for in英文表示在什么什么內(nèi)。 range英文表示范圍大小的。 這句話主要意思是:在變量名內(nèi)的范圍大小是0到5,然后打印變量名。

當(dāng)然為了方便理解,我將變量名寫了中文,實際使用中,變量名=自定義(英文) 另外,range( ):內(nèi)主要填計數(shù)范圍,這個范圍按慣例一般從0開始,并且程序會在我們給的數(shù)字前一位提前結(jié)束,所以在使用的時候需要格外注意。

2.2 range()的特殊用法:

for 變量名 in range(0,10,2):
    print(變量名)

0
2
4
6
8

for 變量名 in range(10,0,-2):
    print(變量名)

10
8
6
4
2

特殊用法就是,在范圍后面加上正數(shù)或者負(fù)數(shù),然后循環(huán)的時候就會以加的這個數(shù)跳著顯示.

2.2 不加range的用法

for 變量名 in [0,1,2,3,4,5]:
    print(變量名)

0
1
2
3
4
5

這種比較小眾,主要是通過列表進行次數(shù)循環(huán),小眾原因是你要循環(huán)多少次就需要在列表內(nèi)寫多少次,比較麻煩,不推薦。

3.whlie條件循環(huán)

  • 我有次在酒桌上問朋友你能喝多少,這個時候朋友微笑的伸出一根手指,
  • 我驚訝道:“1瓶?” 她笑著搖搖頭。
  • “10瓶?” 也不是,
  • “猜不出來了”我趴在桌子上望著她。
  • 于是她認(rèn)真的看著我:“一直喝”
  • ·······(囧)

上面那段,是我親身經(jīng)歷的,我覺得很適合形容whlie語句,不多說,看代碼:

num=int(input("你猜我能喝多少:"))
while num:
    print("我能一直喝")
  • 你猜我能喝多少:2
    - 我能一直喝
    - 我能一直喝
    - ····

寫這一段,其實是想讓大家明白一個道理,雖然我們目前學(xué)習(xí)的程序還很簡單,看上去干不了什么事情,但是只要將編程語言的語法特征大概掌握了,你就擁有了很大的自由,生活的方方面面其實都是可以用計算機按照自己的期望變?yōu)槌绦虻摹?/p>

3.1 給while循環(huán)設(shè)置條件

num=int(input("你猜我能喝多少:"))
while num<10:
    num=num+1
    print(num)
  • 你猜我能喝多少:1
    - 2
    - 3
    - 4
    - 5
    - 6
    - 7
    - 8
    - 9
    - 10

4.讓循環(huán)停下的方法

4.1 break語句 是在循環(huán)語句執(zhí)行到滿足某個條件后,跳出整個循環(huán)。

for zhangzexiang in range(0,6):
    if zhangzexiang==3:
        break
    print(zhangzexiang)

0
1
2

4.2 continue語句 跳過當(dāng)前循環(huán)的剩余語句,然后繼續(xù)進行下一輪循環(huán)。

for zhangzexiang in range(0,6):
    if zhangzexiang==2:
        continue
    print(zhangzexiang)

0
1
3
4
5

5.練習(xí)

5.1 做一個乘法表

這是一個還沒有將乘法結(jié)果加上去的乘法表。讓乘法表左邊執(zhí)行輸入-輸出(代碼行1) 右邊執(zhí)行while循環(huán)-輸出(代碼行2-5)

num_one=int(input("請輸入乘法表:"))
num_two=1
while num_two<=5:
    print("%r×%r"%(num_one,num_two))
    num_two=num_two+1
  • 輸入乘法表1
    - 1×1
    - 1×2
    - 1×3
    - 1×4
    - 1×5

這是一個將乘法結(jié)果(代碼行4)加上去的完整乘法表

num_one=int(input("請輸入乘法表:"))
num_two=1
while num_two<=5:
    num_result = num_one * num_two
    print("%r×%r=%r"%(num_one,num_two,num_result))
    num_two=num_two+1
  • 請輸入乘法表:3
    - 3×1=3
    - 3×2=6
    - 3×3=9
    - 3×4=12
    - 3×5=15

這是一個讓乘法表不斷循環(huán)的乘法表。(代碼行7,開始讓整個乘法表循環(huán))

num_one=int(input("請輸入乘法表:"))
num_two=1
while num_two<=5:
    num_result = num_one * num_two
    print("%r×%r=%r"%(num_one,num_two,num_result))
    num_two=num_two+1
while num_one>=0:
    num_one = int(input("請輸入乘法表:"))
    num_two = 1
    while num_two <= 5:
        num_result = num_one * num_two
        print("%r×%r=%r" % (num_one, num_two, num_result))
        num_two = num_two + 1
  • 請輸入乘法表:1
    - 1×1=1
    - 1×2=2
    - 1×3=3
    - 1×4=4
    - 1×5=5
    - 請輸入乘法表:

讓用戶能決定,乘法表的最大計算范圍

num_one=int(input("請輸入乘法表:"))
num_two=1
num_break=int(input("最大乘到多少:"))
while num_two>0:
    num_result = num_one * num_two
    print("%r × %r = %r"%(num_one,num_two,num_result))
    num_two = num_two + 1
    if num_two== num_break+1:
        break
  • 請輸入乘法表:1
    - 最大乘到多少:5
    - 1 × 1 = 1
    - 1 × 2 = 2
    - 1 × 3 = 3
    - 1 × 4 = 4
    - 1 × 5 = 5

讓這種能自主決定計算范圍的乘法表也能整體循環(huán)

num_one=int(input("請輸入乘法表:"))
num_two=1
num_break=int(input("最大乘到多少:"))
while num_two>0:
    num_result = num_one * num_two
    print("%r × %r = %r"%(num_one,num_two,num_result))
    num_two = num_two + 1
    if num_two== num_break+1:
        break
while num_one>0:
    num_one = int(input("請輸入乘法表:"))
    num_two = 1
    num_break = int(input("最大乘到多少:"))
    while num_two > 0:
        num_result = num_one * num_two
        print("%r × %r = %r" % (num_one, num_two, num_result))
        num_two = num_two + 1
        if num_two == num_break + 1:
            break

  • 請輸入乘法表:1
    - 最大乘到多少:5
    - 1 × 1 = 1
    - 1 × 2 = 2
    - 1 × 3 = 3
    - 1 × 4 = 4
    - 1 × 5 = 5
    - 請輸入乘法表:2
    - 最大乘到多少:5
    - 2 × 1 = 2
    - 2 × 2 = 4
    - 2 × 3 = 6
    - 2 × 4 = 8
    - 2 × 5 = 10
    - 請輸入乘法表:
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號