循环控制

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

  所有的循环都可以嵌套使用

  if判断

    人做事情的时候经常会有判断的过程,那么我们人要机器去做事情也要使得机器也有判断,if就是在这个时候使用的。

    if的语法:

      if 条件:

        code语句1...

        code语句2...

        ....

      实例:

      age=18
      height=167
      weight=95
      sex='female'
      is_beautiful=True
      if age > 16 and age < 30 and height > 165 and weight < 100 and sex == 'female' and is_beautiful:
        print('表白。。。')

      语法2:if....else...

      age=18

      weight=95

      sex='female'

      if age > 16 and age < 30  and weight < 100 and sex == 'female' :
        print('表白。。。')

      else:

        print(“姐姐好..”)

      语法3:多分枝
      强调:if的多分枝=但凡有一个条件成立,就不会再往下判断其他条件了
      if 条件1:
      code1
      code2
      ...
      elif 条件2:
      code1
      code2
      ....  
      else:
      code1
      code2
      ...

      语法4:if嵌套

      age=18
      height=168
      weight=95
      sex='female'
      is_beautiful=True
      if age > 18 and age < 25 and height > 165 and weight < 100 and sex == 'female' :
        print('心动。')

        if  is_beautiful

          print(“表白”)

        else:

          print(“做个好朋友”)

      else:

        print(“姐姐好...”)

    while循环:

      语法:

        a=1

        while True:

          a+=1

          print(a) #这个循环是一个死循环 条件一直为真

        user='lay'
        pwd='123'
        while True:
          inp_user=input('please input your username: ')
          inp_pwd=input('please input your password: ')
          if inp_user == user and inp_pwd == pwd:
            print('login successfull')
          else:
            print('user or password err')
      
        while+break 表示结束本层循环
        while+contin 表示结束本次循环,本次continu之后的代码不会运行,直接进入下一次运行。continu不能放在代码的最后一步
        
        user='lay'
        pwd='123'
        while True:
          inp_user=input('please input your username: ')
          inp_pwd=input('please input your password: ')
          if inp_user == user and inp_pwd == pwd:
            print('login successfull')
            break                #如果账号密码正确 直接打印login successfull 再结束本层循环
          else:
            print('user or password err')

         a=1
         while a<=5
          if a==3:
            continue
          a+=1
         print(a)

        

         

原文地址:https://www.cnblogs.com/layerluo/p/9356464.html