zoukankan      html  css  js  c++  java
  • 流程控制之while循环

    一、语法

    循环就是一个重复的过程,我们人需要重复干一个活,那么计算机也需要重复做一个事情。以ATM为例子吧,ATM验证失败了,它肯定就会让我再次输入密码(虽然只有三次)。这时候就得提出我们牛B的while循环了,while循环又称为条件循环。看下面代码。。

    while条件
    	code 1
      code 2 
      code 3
    

    while True
    print('1'100)
    print('2'100)

    ​```python
    # 实现ATM的输入密码重新输入的功能
    while True:
        user_db = 'nick'
        pwd_db = '123'
    
        inp_user = input('username:')
        inp_pwd = input('password:')
        if inp_user == user_db and inp_pwd == pwd_db :
            print('login successful')
        else:
            print('username or password error')
    
      
    

    上述代码虽然实现了功能,但是用户代码输对了,他也会继续输入。

    二、while + break

    break的意思是终止掉当前面一层的循环,执行其他的代码。

    while True:
      print('1')
      print('2')
      break
      print('3')
    

    1
    2
    上述代码的break丝毫无意义,循环的目的就是为了让计算机像人一样工作的,循环出来事情,二他直接打印1和2之后就退出来循环了。而我们展示下有意的while + break 代码的组合。

    while True:
      user_db = 'nick'
      pwd_db = '123'
      
      inp_user = input('username:')
      inp_pwd = input('password:')
      if inp_user == user_db and inp_pwd == pwd_db:
        print('login successful!!!!')
        break
      else:
        print('username or password error')
        
        
        print('退出了while循环')
    

    username :nick

    password: 123

    login successful

    退出了while循环

    三、while + continue

    continue的意思就是终止本次循环,直接进入下一次循环

    n = 1
    while n < 4:
      print(n)
    

    1 2 3

    n = 1
    while n < 10 :
      if n==8:
        # n +=1 #如果注释这一行,则会进入死循环
        continue
      print(n)
      n +=1
    

    1 2 3 4 5 6 7

    continue不能加载循环体的最后一步执行代码,因为代码加上去毫无意义,如下所示的continue所在的位置就是毫无意义的。ps:注意最后一步执行的代码,而不是最后一行。

    while True
    	if 条件1:
        	code1
          code2
          code3
          ...
       else:
        	code1
          code2
          code3
          ...
          
      continue
    

    四、while循环的嵌套

    ATM密码输入成功后还需要进行一系列的命令操作,比如取款,转账。并且在执行功能结束后会推出命令操作的功能,即在功能出执行输入q会推出输出功能的while循环并且推出ATM程序

    # 退出内层循环的while循环嵌套
    while True:
        user_db = 'nick'
        pwd_db = '123'
    
        inp_user = input('username: ')
        inp_pwd = input('password: ')
    
        if inp_user == user_db and pwd_db == inp_pwd:
            print('login successful')
    
            while True:
                cmd = input('请输入你需要的命令:')
                if cmd == 'q':
                    break
                print(f'{cmd} 功能执行')
        else:
            print('username or password error')
    
    print('退出了while循环')
    
    # 退出双层循环的while循环嵌套
    while True:
        user_db = 'nick'
        pwd_db = '123'
    
        inp_user = input('username: ')
        inp_pwd = input('password: ')
    
        if inp_user == user_db and pwd_db == inp_pwd:
            print('login successful')
    
            while True:
                cmd = input('请输入你需要的命令:')
                if cmd == 'q':
                    break
                print(f'{cmd} 功能执行')
            break
        else:
            print('username or password error')
    
    print('退出了while循环')
    username: nick
    password: 123
    login successful
    请输入你需要的命令:q
    退出了while循环
    
    # tag控制循环退出
    tag = True
    while tag:
        user_db = 'nick'
        pwd_db = '123'
    
        inp_user = input('username: ')
        inp_pwd = input('password: ')
    
        if inp_user == user_db and pwd_db == inp_pwd:
            print('login successful')
    
            while tag:
                cmd = input('请输入你需要的命令:')
                if cmd == 'q':
                    tag = False
                print(f'{cmd} 功能执行')
        else:
            print('username or password error')
    
    print('退出了while循环')
    

    username: nick password: 123 login successful 请输入你需要的命令:q q 功能执行 退出了while循环

    六、while + else

    while+else:else会在while没有被break时才会执行else中的代码。

    # while+else
    n = 1
    while n < 3:
        print(n)
        n += 1
    else:
        print('else会在while没有被break时才会执行else中的代码')
    

    1 2

    else会在while没有被break时才会执行else中的代码

  • 相关阅读:
    HDU1260DP
    HDU1114 背包
    HDU1078记忆化搜索
    HDU1024 最大m子段和
    Codeforces Round #401 (Div. 2) A,B,C,D,E
    HDU3666 差分约束
    HDU1540 区间合并
    HDU3308 线段树(区间合并)
    Codeforces Round #403 (Div. 2) B 三分 C dfs
    HDU1573 线性同余方程(解的个数)
  • 原文地址:https://www.cnblogs.com/luodaoqi/p/11285368.html
Copyright © 2011-2022 走看看