7-2 if判斷 while循環以及for循環(流程控制)

一.if判斷

if判斷的做用:

計算機對事物的好壞,可行性真假對錯等進行一個判斷,從而作出不一樣的處理。python

if判斷的基本格式:

if 條件:
  代碼塊1
  代碼塊2
  代碼塊3
  ....

 

if語句執行基本狀況:

先對條件進行判斷,若是條件符合則會執行if下屬代碼塊,若是不符則不會執行。以下例:先進性條件判斷,當條件符合時,會進行代碼塊1,2,3的執行。若條件不符合則會進行代碼塊4。微信

eg:ide

if 條件:
    代碼塊1
    代碼塊2
    代碼塊3
代碼塊4
View Code

ps:1.在python中用縮進表示代碼的歸屬,同一縮進的代碼稱爲代碼塊spa

     2.同一級別的代碼從上到下依次運行 3d

if判斷的擴展:

1.if....else

if 條件:
    代碼塊1
    代碼塊2
    代碼塊3
else:
    代碼塊4
    代碼塊5
    代碼塊6
View Code

ps:1.當if和else合用時只能執行其中一個code

     2.else不能單獨使用,else只能和if,while,for配合使用 blog

2.if.....elif.....elif....(能夠存在任意個elif)

if 條件:
    代碼塊1
    代碼塊2
    代碼塊3
elif 條件:
    代碼塊1
    代碼塊2
    代碼塊3
View Code

其中if和elif爲同一縮進代碼,從上到下依次進行判斷執行,且也只會執行其中的一個索引

3.if....elif.....else

gender = 'female'
age = 34
is_beautiful = True

if gender == 'female' and age > 18 and age < 30 and is_beautiful:
    print('能不能加個微信啊,我很鐘意你')
elif gender == 'female' and is_beautiful:
    print('考慮一下')
else:
    print('什麼玩意兒')

此案例中if....elif.....else是同一級別的因此從上到下依次進行判斷且只會執行其中的一個內存

4.if的嵌套

View Code

補充:1.在進行判斷的時候,0’,‘[]’,'{}',‘’,None對應的bool值是Falseinput

   2.變量名也是能夠進行判斷的,由於變量名指向的值對應的就是True或者False

   3.布爾值能夠用來直接判斷

二。while循環

while的基本語法:

while 條件:
    代碼塊1
    代碼塊2
    ......

while語句執行順序:先對while的條件進行判斷,當條件知足後會依次執行while的下屬代碼塊,當while的下屬代碼塊執行完後會再次進行while的條件判斷,如此往復執行,直至條件不知足程序終止。

while的拓展運用:

1.while....break

while 條件:
    代碼塊1
    代碼塊2
    ......
    break
View Code

break:break在和while連用的時候做用爲徹底終止循環(針對break所屬的while)

2.while....continue

while 條件:
    代碼塊1
    代碼塊2
    ......
    continue
View Code

continue:while和continue連用,continue的做用是結束本層循環,繼續執行後面的循環。

3.while.....else

n = 1
while n < 5:
    if n == 3:
        break
    print(n)
    n += 1
else:
    print('while循環正常結束了')
View Code

ps:當while和else連用時,當while正常執行完,中間沒有被break停止的話就會執行後面的else

補充:

1.定義一個全局的標誌位,經過更改標誌位來結束程序

tag = True
while tag:
    代碼塊1
    代碼塊2
    ...
    while tag:
        代碼塊1
        代碼塊2
        while tag:
            代碼塊1
            ...
View Code

經過定義一個標誌位tag=True,在後期經過改變獲得tag=false來結束程序

2.不要在程序中寫入死循環

while True:
    1+2+2

由於while的條件一直爲真,程序會一直執行下去並不會報錯

三。for循環

for循環的基本語法結構:

for 變量名 in 容器類型:
    for循環體內的代碼1,
    for循環體內的代碼2,
    ...

for循環用於取值:

1.從列表中取值

l = [1, 2, 3, 4, 5]
for a in l:
    print(a)

其中是把l中的元素一個個賦值給a,而後在把a輸出。

2.從字典中取值

d = {'name':'zhang','password':'123','hobby':[1,2,3,4]}
for i in d:
    print(d)
   print(d[i])

ps:字典暴露給咱們能操做的只有key,因此上述輸出的是key,如需知道value是多少須將最後的輸出改成print(d[i])

        for循環取值不依賴索引,而while循環取值依賴索引。

3.範圍中取值(range)

for a in range(1,10): print(a)

ps:1.在從range中取值時,範圍是顧頭不顧尾的即1能夠取到可是10取不到。

     2. 在python2和python3中range()是不一樣的。在python2中range以列表的形式體現所有的值,佔內存較大,python3中是一個迭代器,佔用空間小,利用率高(你須要的時候纔給你)。在python2中有xrange           功能和python3中的range相同,可是存在一個範圍。

python2和3的不一樣有:range(xrange),input(raw_input)

4.for....break    for....continue    for...else功能和while循環中是同樣的。

5.for循環的嵌套

eg:乘法口訣表

for i in range(1,10):
    for j in range(1,i+1):  # 內存循環的range條件是根據外層循環決定的
        print('%s*%s=%s'%(i,j,i*j),end=' ')
    print()

今日做業:

1.限制用戶登陸錯誤嘗試次數

 

use_info = {
    'name':'zhang','pwd':'123'
}
a = 1
while a < 4:
    name = input('please input your name>>>:')
    pwd = input('please input your pwd>>>:')
    if name == use_info['name'] and pwd == use_info['pwd']:
        print('謝謝翻牌')
        break
    a+=1
View Code

 

2.用戶嘗試三次還不對後直接結束程序用戶嘗試三次輸入不對後提示用戶是否繼續嘗試,若是輸入y則再給三次機會,若是輸入n則直接結束程序

use_info = {
    'name':'zhang','pwd':'123'
}
a = 1
while a < 4:
        name = input('please input your name>>>:')
        pwd = input('please input your pwd>>>:')
        if name == use_info['name'] and pwd == use_info['pwd']:
            print('謝謝翻牌')
            break
        else:
            a += 1
            if a == 4:
                print('若是你想繼續登錄請輸入"y",離開可輸入"n"')
                select = input('please input "y" or "n">>>:')
                if select == 'y':
                    a = 1
                else:
                    break
View Code
use_info = {
    'name':'zhang','pwd':'123'
}
a = 1
while a < 4:
        name = input('please input your name>>>:')
        pwd = input('please input your pwd>>>:')
        if name == use_info['name'] and pwd == use_info['pwd']:
            print('謝謝翻牌')
            while True:
                cmd = input('please input cmd>>>:')
                if not cmd:continue
                if cmd == 'q':break
                else:print('run <%s>' %cmd)
            break
        else:
            a += 1
            if a == 4:
                print('若是你想繼續登錄請輸入"y",離開可輸入"n"')
                select = input('please input "y" or "n">>>:')
                if select == 'y':
                    a = 1
相關文章
相關標籤/搜索