while循环

语法:
while 条件:
  代码1
  代码2
  代码3python

 

while True:
  name=input('please input your name: ')
  pwd=input('please input your password: ')

  if name == 'egon' and pwd == '123':
    print('login successful')
  else:
    print('username or password error')

  

结束while循环的两种方式blog

方式一:条件改成False,
在条件改成False时不会当即结束掉循环,而是要等到下一次循环判断条件时才会生效input

tag=True
while tag:
    name=input('please input your name: ')
    pwd=input('please input your password: ')

    if name == 'egon' and pwd == '123':
        print('login successful')
        tag=False
    else:
        print('username or password error')

    print('===>')                

  


方式二:while+break
break必定要放在循环体内,一旦循环体执行到break就会当即结束本层循环class

while True:
    name=input('please input your name: ')
    pwd=input('please input your password: ')

    if name == 'egon' and pwd == '123':
        print('login successful')
        break
    else:
        print('username or password error')

    print('===>>>>>')
    print('===>>>>>')        

  

while+continue:结束本次循环,直接进入下一次循环循环

# 示例一语法

count=1
while count < 6: #count=6
    if count == 4:
        count += 1
        continue

    print(count)
    count+=1

  

# 示例二:error

while True:
    name=input('please input your name: ')
    pwd=input('please input your password: ')

        if name == 'egon' and pwd == '123':
        print('login successful')
        break
    else:
        print('username or password error')
# continue # 此处加continue无用        

  

了解知识
while + else:查询

while 条件:
  代码1
  代码2
  代码3
  else:
在循环结束后,而且在循环没有被break打断过的状况下,才会执行else的代码word

tag=True
while tag:
  print(1)
  print(2)
  print(3)
# tag=False
  break
  else:
    print('else的代码')

  

while 条件1:
while 条件2:
代码1
代码2
代码3di

示范一:

while True:
    name=input('please input your name: ')
    pwd=input('please input your password: ')

    if name == 'egon' and pwd == '123':
        print('login successful')
        while True:
            print("""
                0 退出
                1 取款
                2 转帐
                3 查询
                """)
            choice=input('请输入您要执行的操做:') #choice='1'
            if choice == '0':
                break
            elif choice == '1':
                print('取款。。。')
            elif choice == '2':
                print('转帐。。。')
            elif choice == '3':
                print('查询')
            else:
                print('输入指令错误,请从新输入')
                break
      else:
      print('username or password error')

  

# 示范二:

tag=True
while tag:
    name=input('please input your name: ')
    pwd=input('please input your password: ')

    if name == 'egon' and pwd == '123':
        print('login successful')
        while tag:
            print("""
                0 退出
                1 取款
                2 转帐
                3 查询
                """)
            choice=input('请输入您要执行的操做:') #choice='1'
            if choice == '0':
            tag=False
            elif choice == '1':
                print('取款。。。')
            elif choice == '2':
                print('转帐。。。')
            elif choice == '3':
                print('查询')
            else:
                print('输入指令错误,请从新输入')
    else:
    print('username or password error')
相关文章
相关标签/搜索