流程控制之while循环语句

 流程控制之while循环语句

1.while+break练习

 n=0
 user='egon'
 password='meiyou'
 while n<=3:
     user_input=input('please input your name: ')
     password_input=input('please input your password:')
     if user_input==user and password_input==password:
         print('welcome!')
         break
     else :
         print('Error!')
     n+=1

 2.while+continue练习
 n=1
 while n<=10:
     if n==6:
         n+=1
         continue
     print(n)
     n+=1

 3.while True:
     if 条件1:
         code1
         code2
         code3
         continue #无意义
     elif 条件1:
         code1
         continue #有意义
         code2
         code3
     elif 条件1:
         code1
         code2
         code3
         continue #无意义
     ....
     else:
         code1
         code2
         code3
         continue #无意义

 4.while嵌套练习
 user='xiaoming'
 password='aixiaohong'
 while True:
     user_input=input('please input your name :')
     password_input=input('please input your password: ')
     if user_input==user and password_input==password:
         print('welcome')
         while True:
             cmd=input('请输入您要执行的功能:')
             if cmd=='q':
                 break
             print('%s 功能正在执行...'%cmd)
         break
     else  :
         print('user or password is error!')
 print('end...')

 5.while+tag
 user='xiaoming'
 password='aixiaohong'
 tag=True
 while tag:
     user_input=input('please input your name :')
     password_input=input('please input your password: ')
     if user_input==user and password_input==password:
         print('welcome')
         while tag:
             cmd=input('请输入您要执行的功能:')
             if cmd=='q':
                 break
             print('%s 功能正在执行...'%cmd)
         break
     else  :
         print('user or password is error!')
 print('end...')

6.while+break
n=1
while n < 5:
    # if n == 3:
    #     break
    print(n)
    n+=1
else:
    print('在整个循环结束后,会进行判断:只有while循环在没有被break结束掉的情况下才会执行else中的代码')

猜你喜欢

转载自blog.csdn.net/u014297588/article/details/80503308
今日推荐