Python05:while循環

If…else的進階,用幾個程序來實現講解。python

 

if猜數字:spa

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

age = 56

guess_age = int(input("guess age:"))

if guess_age == age:
    print("yes, you got it.")
elif guess_age > age:
    print("think smaller...")
else:
    print("think bigger...")code

 

輸出:utf-8

guess age:23input

think bigger...it

解釋:table

如要須要判斷多個條件,在if…else中添加elif須要多少添加多少,如上文程序。class

可是,這樣程序只能執行一次就結束了,加入次數條件控制(程序的循環執行)。cli

 

while實現循環:coding

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

count = 0
while True:
    print("count:", count)
    count = count + 1 #count += 1

 

輸出:

count: 102609

count: 102610

count: 102611

count: 102612…

解釋:

    while條件爲真(True永遠爲真),代碼塊就一直執行(程序無限執行)。(程序中count +=1count = count + 1是等價的)。

 

加入while條件循環的猜數字:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

age = 56



while True:
    guess_age = int(input("guess age:"))
    if guess_age == age:
        print("yes, you got it.")
    elif guess_age > age:
        print("think smaller...")
    else:
        print("think bigger...")

 

輸出:

guess age:56

yes, you got it.

guess age:23

think bigger...

解釋:

    不管猜對錯,程序無限執行,很差,改進一下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

age = 56



while True:
    guess_age = int(input("guess age:"))
    if guess_age == age:
        print("yes, you got it.")
        break
    elif guess_age > age:
        print("think smaller...")
    else:
        print("think bigger...")

 

輸出:

guess age:67

think smaller...

guess age:56

yes, you got it.

 

Process finished with exit code 0

解釋:

break:退出,結束循環。

此時程序猜對退出執行,但猜錯的話仍是無限猜下去,仍是很差,再改進:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

age = 56

count = 0

while True:
    if count == 3:
        break
    guess_age = int(input("guess age:"))
    if guess_age == age:
        print("yes, you got it.")
        break
    elif guess_age > age:
        print("think smaller...")
    else:
        print("think bigger...")

    count += 1

 

輸出:

guess age:1

think bigger...

guess age:2

think bigger...

guess age:3

think bigger...

 

Process finished with exit code 0

解釋:

程序猜對,或者猜錯三次結束執行,可是仍是很low

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

age = 56

count = 0

while count < 3:
    guess_age = int(input("guess age:"))
    if guess_age == age:
        print("yes, you got it.")
        break
    elif guess_age > age:
        print("think smaller...")
    else:
        print("think bigger...")

    count += 1

 

程序稍好看一點點,輸出略,同上一個代碼是同樣的功能,下面while再添加一個else

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

age = 56

count = 0

while count < 3:
    guess_age = int(input("guess age:"))
    if guess_age == age:
        print("yes, you got it.")
        break
    elif guess_age > age:
        print("think smaller...")
    else:
        print("think bigger...")

    count += 1
else:
    print("you have tried too many times... fuck off")

 

輸出:

guess age:1

think bigger...

guess age:2

think bigger...

guess age:3

think bigger...

you have tried too many times... fuck off

 

Process finished with exit code 0

解釋:

輸入錯誤三次纔打印print語句,while後能夠接else語句,意思是若是while正常走完了,那麼執行else後的代碼塊,不然break結束循環了就再也不執行else後的代碼塊。

相關文章
相關標籤/搜索