二、流程控制之while循环与for循环
1 什么是循环
循环就是一个重复的过程
2 为何要有循环
人可以重复的去做某一件事
程序中必须有一种机制能够控制计算机像人一样重复地去做某一件事
3 如何用循环
# 语法
# while 条件:
# code1
# code2
# code3
# ...
一、while 循环
whileTrue:inp_user=input('pleaseinputyourusername:')inp_pwd=input('pleaseinputyourpassword:')ifinp_user=="user"andinp_pwd=="123":print('loginsuccessfull')else:print('userorpassworderr')
# while + break: break代表结束本层循环
whileTrue:inp_user=input('pleaseinputyourusername:')inp_pwd=input('pleaseinputyourpassword:')ifinp_user=="user"andinp_pwd=="123":print('loginsuccessfull')breakelse:print('userorpassworderr')
# while+continue:continue代表结束本次循环(本次循环continue之后的代码不在运行),直接进入下一次循环
# 强调:continue一定不要作为循环体的最后一步代码
start=1whilestart<8:#5<8ifstart==4:start+=1#start=5continueprint(start)start+=1
user_from_db='egon'pwd_from_db='123'whileTrue:inp_user=input('pleaseinputyourusername:')inp_pwd=input('pleaseinputyourpassword:')ifinp_user==user_from_dbandinp_pwd==pwd_from_db:print('loginsuccessfull')breakelse:print('userorpassworderr')continue
#while循环的嵌套
user_from_db='egon'pwd_from_db='123'whileTrue:inp_user=input('pleaseinputyourusername:')inp_pwd=input('pleaseinputyourpassword:')ifinp_user==user_from_dbandinp_pwd==pwd_from_db:print('loginsuccessfull')whileTrue:cmd=input('>>>:')#cmd='quit'ifcmd=='quit':breakprint('%srun......'%cmd)breakelse:print('userorpassworderr')
给tag打标:
user_from_db='egon'pwd_from_db='123'tag=Truewhiletag:inp_user=input('pleaseinputyourusername:')inp_pwd=input('pleaseinputyourpassword:')ifinp_user==user_from_dbandinp_pwd==pwd_from_db:print('loginsuccessfull')whiletag:cmd=input('>>>:')#cmd='quit'ifcmd=='quit':tag=Falsebreakprint('%srun......'%cmd)else:print('userorpassworderr')
# while + else
# else的代码会在while循环没有break打断的情况下最后运行
n=1whilen<5:ifn==4:breakprint(n)n+=1else:print('=====》')
密码数错3次程序退出
user_from_db='egon'pwd_from_db='123'count=0tag=Truewhiletag:ifcount==3:print('输错次数过多')breakinp_user=input('pleaseinputyourusername:')inp_pwd=input('pleaseinputyourpassword:')ifinp_user==user_from_dbandinp_pwd==pwd_from_db:print('loginsuccessfull')whiletag:cmd=input('>>>:')#cmd='quit'ifcmd=='quit':tag=Falsebreakprint('%srun......'%cmd)else:print('userorpassworderr')count+=1#count=3#输错3次
二、流程控制之for循环
names=['egon','alex_dsb','lxx_sb','yxx_dsb']i=0whilei<len(names):#4<4print(names[i])i+=1
# for循环:可以不依赖索引而取指
names=['egon','alex_dsb','lxx_sb','yxx_dsb']foriteminnames:#item='lxx_sb'print(item)
dic={'x':1,'y':2,'z':3}forkindic:#k='x'print(k,dic[k])
# for vs while
# for可以不依赖于索引取指,是一种通用的循环取指方式
# for的循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的
names=['egon','alex_dsb','lxx_sb','yxx_dsb']foriinrange(0,len(names)):print(names[i])
# for+break
# for+continue
# for+else
foriinrange(10):ifi==4:breakprint(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
'''
foriinrange(1,10):#i=3forjinrange(1,i+1):print('%s*%s=%s'%(i,j,i*j),end='')#i=2j=2print()
''' 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=10forcurrent_levelinrange(1,max_level+1):#先不换行打印打印空格forxinrange(max_level-current_level):print('',end='')#再不换行打印*foryinrange(2*current_level-1):print('*',end='')print()
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。