Python基礎部分2——If 條件語句與循環(While循環For循環)

一.if條件語句

語法格式:
    if  
        條件1...  :(冒號結尾)  #如果不滿足,則執行下一條
        if        條件n...  :(冒號結尾)  #如果不滿足,則執行下一條
        elif     條件n...  :(冒號結尾)  #如果不滿足,則執行下一條
        else    如果不滿足條件那麼就輸出...
    elif  
        條件2...  :(冒號結尾)  #如果不滿足,則執行下一條
    else  :(冒號結尾)
        如果不滿足條件那麼就輸出...

例子:if條件語句

gender = 'female'
age = input('age >>:')
age=int(age)
height = input('height >>:')
height=int(height)
is_beautiful = True

if gender == 'female' and age > 18 and age <= 25 and height > 160 and is_beautiful:
    print('表白中...')
else:
    print('阿姨好...')

  練習1:用戶登陸驗證

name=input('pls input you name>>:')
passwd=input('pls input you passwd>>:')
passwd=int(passwd)   #輸入的數據設置爲整數類型

if name == 'szq' and passwd == 666666:
    print('Welcome')
elif name != 'szq' or passwd != 666666:
    print('You name or passwd error')
else:
    print('The name does not exist')

  練習2:根據用戶輸入的內容給出其權限

name=input('pls input you name>>:')

if name == 'szq':
    print('超級管理員')
elif name == 'wjp':
    print('網管')
elif name == 'zyl' or name == 'wxx':
    print('技術專員')
else:
    print('不存在這個人')

  練習3:判斷今天是周幾,然後決定是出去玩還是上班

today=input('pls input today>>:')
today=int(today)

if today in [1,2,3,4,5]:
    print('好好上班,別想着出去玩')
elif today in [6,7]:
    print('出去玩')
else:
    print('''
    必須輸入:1,2,3,4,5,6,7其中一種
    ''')

注意點:if today in [1,2,3,4]:  就相當於變量today的值存在與[1,2,3,4]內。    #變量today匹配[]中括號內的任意一個值。

 練習4:用戶登陸接口

    讓用戶輸入用戶名密碼

    認證成功後顯示歡迎信息

    輸錯三次後退出程序

user_list = {
    'ssp': {'passwd': 666666, 'account': 0},
    'wsp': {'passwd': 666666, 'account': 0},
    'zsp': {'passwd': 666666, 'account': 0},
}
while True:
    name = input('pls input you name >>:')
    passwd = input('pls input you passwd >>:')
    passwd=int(passwd)

    if name in user_list and passwd != user_list[name]['passwd']:
        print('用戶或密碼輸入錯誤,請重新輸入')
        user_list[name]['account'] += 1  # 針對不同用戶做失敗次數判斷,這樣失敗次數不會疊加,某一用戶失敗,其他用戶不受影響
        if user_list[name]['account'] > 2:  # 如果某一用戶失敗次數大於3,那麼就提示用戶"系統將被鎖定"
            print('你嘗試的輸入的次數過多,系統已被鎖定')
            break
        continue
    elif name in user_list and passwd == user_list[name]['passwd']:
        print('登陸成功')
        break
    else:
        print('用戶名不在')
        exit()

練習5:登陸接口需求升級:

    可以支持多個用戶登錄 (提示,通過列表存多個賬戶信息)

    用戶3次認證失敗後,退出程序,再次啓動程序嘗試登錄時,還是鎖定狀態(提示:需把用戶鎖定的狀態存到文件裏)

前言:這裏需要用到Python中文件的讀取和寫入功能,例:

user_list={
    'ssp':{'passwd':123,'account':0},
    'wsp':{'passwd':123,'account':0},
    'zsp':{'passwd':123,'account':0},
}

while True:
    name=input('pls input you name >>:')
    passwd=input('pls input you passwd >>:')
    passwd=int(passwd)

    with open('user_list.txt','r') as f:          #打開文件"test.txt","r"表示只讀,默認使用
        user_land=f.read()                        #f.read()表示讀取文件"test.txt"得內容並賦值給變量"user_land"
    if name in user_land:
        print('用戶 %s 嘗試登陸次數過多,已被系統鎖定,無法登陸!' % (name))
        break

    if name in user_list and passwd != user_list[name]['passwd']:
        print('用戶或密碼輸入錯誤,請重新輸入')
        user_list[name]['account'] += 1
        if user_list[name]['account'] > 2:
            print('你嘗試的輸入的次數過多,系統已被鎖定')
            with open('user_list.txt', 'w') as f:    #"w"表示可寫,默認使用"r"只讀,可寫模式下不能讀文件內容
                f.write(name)                        #要寫得內容
                f.close()                            #有打開就有關閉
            break
        continue
    elif name in user_list and passwd == user_list[name]['passwd']:
        print('登陸成功')
        break
    else:
        print('用戶名不在')

   Python讀寫功能註釋:

Python讀寫功能註釋
with open('test.txt','w') as f:    #打開文件"test.txt","w"表示可寫,默認使用"r"只讀,可寫模式下不能讀文件內容
    f.write(name)                  #要寫得內容
    f.write(passwd)                #要寫得內容
    f.close()                      #有打開就有關閉

with open('test.txt') as f:
    user_list=f.read()             #f.read()"讀取文件test.txt得內容",把值賦值給變量user_list
if name in user_list:
    print('用戶%s被鎖定' %(name))

 

二.循環 While與for循環

    2.1while 循環

    2.1.1 while循環的語法(又稱之爲條件循環)
        while 條件:
            代碼1
            代碼2
            代碼3
            。。。。

    2.1.2 while+break,break的意思是結束本層循環

    2.1.2 while+continue,continue的意思是結束本次循環,進入下一次循環

    while+continue練習:打印數字1-10,其中不打印數字8

n=1
while n <= 10:
    if n == 8:            
        n+=1              #這裏不加n+=1的話,while循環執行到n=8時,會一直循環,那麼只能打印1-7,在8這個位置卡住
        continue          #當n=8時跳出本次循環
    print(n)
    n+=1

     練習1:循環嵌套 (while循環內再加while循環)

while True:                       #第1個while循環
    print('第一層')
    while True:                   #第2個while循環
        print('第二層')
        while True:               #第3個while循環
            cmd=input('第三層>>: ')
            if cmd == 'q':
                break             #如果在第3個while循環內輸入'q',則跳出本層(第3層)循環
        break
    break

    練習2:循環嵌套小技巧  while+tag  #適合用於循環嵌套(即while循環中套多個while循環,3個以上循環嵌套比較適合用)

tag=True                #定義變量tag=True
while tag:
    print('第一層')
    while tag:
        print('第二層')
        while tag:
            cmd=input('第三層>>: ')
            if cmd == 'q':          #當輸入'q'命令時
                tag = False         #定義變量tag爲False,那麼本層循環結束後,所有的變量tag的值都爲False,那麼所有的循環都將結束
                break

    2.2 for循環  方便從一個數據裏面取多個值出來

  例子1、for循環字符串

ame='szq'
for n in name: #相當於:for n in 'szq'
print(n) #運行後得到如下值
s
z
q

  例子2、for循環列表

list=[1,2,3]
for n in list:
    print(n)

1
2
3

  例子3、for循環字典

user_list={
    'aaa':{"passwd":123,"account":0},
    'bbb':{"passwd":123,"account":0},
    'ccc':{"passwd":123,"account":0},
}

for key in user_list:
    print(key)
    
aaa
bbb
ccc

  例子4、for循環多個字典

user_list={
    'aaa':{"passwd":123,"account":0},
    'bbb':{"passwd":123,"account":0},
    'ccc':{"passwd":123,"account":0},
}

user={
    'a':12,
    'b':12,
    'c':12,
}

for k,v in zip(user_list,user):
    print(k,v)

aaa a
bbb b
ccc c

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章