流程控制

  1. 条件语句:

    • if语句:

      • if 布尔判断语句:
        	布尔判断语句为true时执行的代码
        
      • import random
        connect = input("请输入你猜的数: ")
        count = random.randint(0,10)   #产生1~10的随机数
        while 1:
            if int(connect) == count:
                print("%s,你猜对了" % connect)
            if int(connect) != count:
                print("%s,你猜错了" % count)
                break
        
    • if ... else ... 语句;
      • if 布尔判断语句:
        	布尔判断语句为true时执行的代码
            
         else:
            布尔判断语句为false时执行的语句
        
      • 对上面的案例进行修改:

        • import random
          connect = input("请输入你猜的数: ")
          count = random.randint(0,10)   #产生1~10的随机数
          while 1:
              if int(connect) == count:
                  print("%s,你猜对了" % connect)
              else:
                  print("%s,你猜错了" % count)
                  break
          
    • 多重if判断:
      • if 布尔判断语句1:
        	布尔判断语句1为true时执行的代码
        	
        elif 布尔判断语句2:
        	布尔判断语句2为true时执行的代码
        	
        	.....
        	
         else:
            上面的布尔判断都为false时执行的语句
        
        
      • # 根据预算金额选购车辆
        # 预算>100万 奔驰s级
        # 预算>50万 奔驰5系
        # 预算>10万 奥迪A4l
        # 预算<10万 捷安特
        while 1:
            print("输出q或者Q退出程序")
            money = input("你预算有多少钱(万): ")
            if int(money) > 100:
                print("奔驰s级: ")
            elif int(money) > 50:
                print("奔驰5系")
            elif int(money) > 10:
                print("奥迪A4l")
            elif 0 < int(money)  < 10:
                print("捷安特")
            elif money.upper() == "Q":
                print("退出程序")
                break
            else:
                print("输入错误")
        
    • if嵌套:
      • if 布尔表达式1:
        	布尔判断语句1为true时执行的代码
        	
        	if 布尔表达式2:
        	布尔判断语句2为true时执行的代码 
        
      • while 1:
            GoHome = input("请选择回家的路径(A为大路,B为小路,C为绕路):")
            if GoHome.upper() == "A":
                print("走大路回家")
                method = input("请选择回家的方法(公交车,步行): ")
                if method == "公交车":
                    print("10分钟到家")
                    break
                elif method == "步行":
                    print("20分钟到家")
                    break
            elif GoHome.upper() == "B":
                print("走小路回家")
                break
            elif GoHome.upper() == "C":
                print("绕路回家")
                play = input("请选择去网吧玩还是游戏厅: ")
                if play == "游戏厅":
                    print("一个半小时到家,爸爸在家,拿棍等你")
                    continue
                elif play == "网吧":
                    print("两个小时到家,妈妈已经做好了准备")
                    continue
        
    • 三目运算符:

      • 三目运算符又叫三元运算符或三元表达式:

      • 条件成立之的表达式 if条件 else 条件不成路执行的表达式
        
        • a = 100
          b = 20
          c = a if a<b else b
          
  2. 循环:

    • while循环:

      • while 布尔判断语句:
        	条件为true重复执行的代码
        
      • #输出100遍 hello world
        
        count = 0
        while True:
            if count < 100:
                print("hello, world")
            count += 1
        
      • #1000以内被5整除的数
        
        count = 0
        while count <= 1000:
            if count % 5 == 0:
                print(count)
            count += 1
        
      • count = 0
        user = 'xxf'
        passwd = '123456xxf'
        while count < 3:
            name = input("请输入用户名: ")
            pwd = input("请输入密码: ")
            count += 1
            if name == user and pwd == passwd:
                print("登陆成功了")
                break
            else:
                print("你输错了,请重新输入,你的机会还有%d次" % (3 - count))
            if count == 3:
                 print("你个傻子,没得玩了")
        
      • break和continue:

        • break 直接退出循环
        • continue 退出本次循环
    • while...else

      • while 布尔表达式:
        	条件成立重复执行的代码
        else:
        	循环正常结束之后要执行的代码
        
      • i = 1
        while i <= 5:
            print("Hello Wrold")
            i += 1
        else:
            print("GoodBye")
        
        
  3. for循环:

    • for 临时变量 in 序列:
      	重复执行的代码1
      
    • str1 = 'insterting'
      for i in str1:
      	print(i)
      
    • for ...else

      • for 临时变量 in 序列:   
        	重复执行的代码1
        	....
        else:
        	循环正常结束之后要执行的代码
        
      • str1 = 'insterting'
        for i in str1:
        	print(i)
        else:
        	print("完了......")
        

posted @ 2021-01-14 11:32  小幸福Y  阅读(34)  评论(0编辑  收藏  举报