扩大
缩小
人生本来就短暂,为什么还要栽培苦涩。
返回顶部

流程控制之 if判断 ,while,for循环练习。

目录

    1. 输入姑娘的年龄后,进行以下判断:

      1. 如果姑娘小于18岁,打印“不接受未成年”
      2. 如果姑娘大于18岁小于25岁,打印“心动表白”
      3. 如果姑娘大于25岁小于45岁,打印“阿姨好”
      4. 如果姑娘大于45岁,打印“奶奶好”
      age_inp = input("请输入你的年龄")
      age_inp_int = int(age_inp)
      if age_inp_int < 18:
          print("不接受未成年")
      elif age_inp_int > 18 and age_inp_int < 25:
          print("心动表白")
      elif age_inp_int > 25 and age_inp_int < 45:
          print("阿姨好")
      else:
          print("奶奶好")
      
    2. 预习while循环,打印1-100之间的奇数和

      count = 1
      total = 0
      while count <=100:
          if count % 2 == 1:
              total += count
          count += 1
      print(total)
      
    3. 预习while循环,猜年龄游戏升级版,有以下三点要求:

      1. 允许用户最多尝试3次
      2. 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
      3. 如果猜对了,就直接退出
      count = 1
      while True:
          age = input("请输入你的年纪")
          age = int(age)
          if age == 21:
              print("你猜对了")
              break
          print("猜错了")
          if count == 3:
              play = input("是否还想继续玩? Y/y/N/n:")
              if play == "Y/y":
                  count = 1
                  continue
              elif play == "N/n":
                  break
              else:
                  print("输入有误")
                  break
          count = count + 1
      
      count = 1
      while count <= 3:
          x = input("请输入")
          timer = 3 - count
          total = "用户名或密码输入错误,剩余%s次机会" %(timer)
          print(total)
          count +=1
      
    posted @ 2019-09-11 16:59  晴天sky  阅读(174)  评论(2编辑  收藏  举报
    左边日期 搜索的制定样式