2、流程控制之while循環與for循環

1 什麼是循環
    循環就是一個重複的過程

2 爲什麼要有循環
    人能夠重複的去作某一件事
    程序中必須有一種機制可以控制計算機像人同樣重複地去作某一件事

3 如何用循環python

# 語法
# while 條件:
#     code1
#     code2
#     code3
#     ...ide

1、while 循環
ui

while True:
    inp_user=input('please input your username: ')
    inp_pwd=input('please input your password: ')
    if inp_user == "user" and inp_pwd == "123":
        print('login successfull')
    else:
        print('user or password err')

# while + break: break表明結束本層循環spa

while True:
    inp_user=input('please input your username: ')
    inp_pwd=input('please input your password: ')
    if inp_user == "user" and inp_pwd == "123":
        print('login successfull')
        break
    else:
        print('user or password err')

# while+continue:continue表明結束本次循環(本次循環continue以後的代碼不在運行),直接進入下一次循環
# 強調:continue必定不要做爲循環體的最後一步代碼code

start=1
while start < 8: #5 < 8
    if start == 4:
        start += 1 #start=5
        continue
    print(start)
    start+=1
user_from_db='egon'
pwd_from_db='123'
while True:
    inp_user=input('please input your username: ')
    inp_pwd=input('please input your password: ')
    if inp_user == user_from_db and inp_pwd == pwd_from_db:
        print('login successfull')
        break
    else:
        print('user or password err')
        continue

#while循環的嵌套對象

user_from_db='egon'
pwd_from_db='123'
while True:
    inp_user=input('please input your username: ')
    inp_pwd=input('please input your password: ')
    if inp_user == user_from_db and inp_pwd == pwd_from_db:
        print('login successfull')
        while True:
            cmd=input('>>>: ') # cmd='quit'
            if cmd == 'quit':
                break
            print('%s run......' %cmd)
        break
    else:
        print('user or password err')

給tag打標:索引

user_from_db='egon'
pwd_from_db='123'
tag=True
while tag:
    inp_user=input('please input your username: ')
    inp_pwd=input('please input your password: ')
    if inp_user == user_from_db and inp_pwd == pwd_from_db:
        print('login successfull')
        while tag:
            cmd=input('>>>: ') # cmd='quit'
            if cmd == 'quit':
                tag=False
                break
            print('%s run......' %cmd)
    else:
        print('user or password err')

# while + else
# else的代碼會在while循環沒有break打斷的狀況下最後運行input

n=1
while n < 5:
    if n == 4:
        break
    print(n)
    n+=1
else:
    print('=====》')

密碼數錯3次程序退出cmd

user_from_db='egon'
pwd_from_db='123'

count=0
tag=True
while tag:
    if count == 3:
        print('輸錯次數過多')
        break
    inp_user=input('please input your username: ')
    inp_pwd=input('please input your password: ')
    if inp_user == user_from_db and inp_pwd == pwd_from_db:
        print('login successfull')
        while tag:
            cmd=input('>>>: ') # cmd='quit'
            if cmd == 'quit':
                tag=False
                break
            print('%s run......' %cmd)
    else:
        print('user or password err')
        count+=1 #count=3 # 輸錯3次

2、流程控制之for循環it

names=['egon','alex_dsb','lxx_sb','yxx_dsb']
i=0
while i < len(names): #4 < 4
    print(names[i])
    i+=1

# for循環:能夠不依賴索引而取指

names=['egon','alex_dsb','lxx_sb','yxx_dsb']
for item in names: #item='lxx_sb'
    print(item)
dic={'x':1,'y':2,'z':3}
for k in dic: #k='x'
    print(k,dic[k])

# for vs while
# for能夠不依賴於索引取指,是一種通用的循環取指方式
# for的循環次數是由被循環對象包含值的個數決定的,而while的循環次數是由條件決定的

names=['egon','alex_dsb','lxx_sb','yxx_dsb']
for i in range(0,len(names)):
    print(names[i])

# for+break
# for+continue

# for+else

for i in range(10):
    if i == 4:
        break
    print(i)
else:
    print('====>')

練習

'''
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
...
9*1=9.................9*9=81
'''

 for i in range(1,10): #i=3
     for j in range(1,i+1):
         print('%s*%s=%s ' %(i,j,i*j),end='') #i=2 j=2
     print()

'''            max_level=5
    *          current_level=1 空格數=4 星號=1
   ***         current_level=2 空格數=3 星號=3
  *****        current_level=3 空格數=2 星號=5
 *******       current_level=4 空格數=1 星號=7
*********      current_level=5 空格數=0 星號=9
'''

max_level=10
for current_level in range(1,max_level+1):
    # 先不換行打印打印空格
    for x in range(max_level-current_level):
        print(' ',end='')
    # 再不換行打印*
    for y in range(2*current_level - 1):
        print('*',end='')
    print()
相關文章
相關標籤/搜索