二、流程控制之while循环与for循环
admin
2023-07-13 02:23:03
0

1 什么是循环
    循环就是一个重复的过程

2 为何要有循环
    人可以重复的去做某一件事
    程序中必须有一种机制能够控制计算机像人一样重复地去做某一件事

3 如何用循环

# 语法
# while 条件:
#     code1
#     code2
#     code3
#     ...

一、while 循环

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代表结束本层循环

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一定不要作为循环体的最后一步代码

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打断的情况下最后运行

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

密码数错3次程序退出

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次

二、流程控制之for循环

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()




相关内容

热门资讯

问题居然在实体卡槽上!美版iP... 6月2日消息,日前,又有博主提前把还没发布的iPhone 18 Pro电池参数给曝光了出来,根据爆料...
2026年618手机购机攻略:... 每年618都是手机换新的最佳窗口期,2026年大促叠加数码国补、平台满减、品牌降价三重福利,新机老机...
8.99万打穿底价!史无前例的... 科技的浪漫,不是将人类送入遥不可及的星辰,而是把曾经高不可攀的科幻,变成柴米油盐里的“论斤卖”。 如...
苹果首款折叠屏iPhone U... PChome 6月2日消息,据知名数码博主@i冰宇宙最新曝光的机模谍照,苹果首款折叠屏手机(内部命名...
特朗普:美伊暂停谈判?假新闻! 当地时间6月2日,美国总统特朗普否认伊朗已停止与调解方沟通,称美国与伊朗之间的对话一直在持续进行。特...
白宫记协晚宴改期至7月,特朗普... 当地时间6月2日,美国总统特朗普表示,此前因突发暴力事件而中断的白宫记协晚宴已确定改期至7月24日举...
特朗普,开掉“不听话的情报头子... 苦撑15个月后,美国国家情报总监加巴德即将离开白宫。她对辞职的解释是:丈夫近期确诊罕见骨癌,自己希望...
面粉染头模仿黄仁勋,农村青年走... “先赔偿5000元肖像侵权使用费,再删除所有视频,要么就起诉你。”这是黄仁勋的模仿者杨洋最近频繁收到...
驻马店非遗“大集”开张 把网络... 顶端新闻记者 王丹/文 李思翰 胡楚昊/图红纸剪出“抵制网络谣言”,糖画写下“不信谣不传谣”……5月...
A股三大指数集体收涨 贵金属涨...   A股三大指数集体收涨 贵金属涨幅居前  【A股三大指数集体收涨 贵金属涨幅居前】6月2日,A股三...