• 第六天 控制流程 之循环


    一、debug调试工具的使用

      F7、F8的使用

      前提条件:需要打断点

      打断点:在行号后面点击一下会出现一个红点,

     打了断点后,使用debug模式运行(代码运行到运行到断点会停下来)
     F7 会进入调用的函数内部执行
     F8 会在当前代码下执行
    import random
    
    a = ('石头', '剪刀', '')
    user_number = int(input("请输入出拳的数字石头(1)/剪刀(2)/布(3):"))
    Punch_number = random.randint(1, 3)
    if user_number - Punch_number == -1 or user_number - Punch_number == 2:
        print('用户赢了', f'用户出{a[user_number - 1]},电脑出{a[Punch_number - 1]}')
    elif user_number - Punch_number == 1 or user_number - Punch_number == -2:
        print('用户赢了', f'用户出{a[user_number - 1]},电脑出{a[Punch_number - 1]}')
    elif user_number - Punch_number == 0:
        print('平局', f'用户出{a[user_number - 1]},电脑出{a[Punch_number - 1]}')
    else:
        print(f"用户出拳{a[user_number]},请重新出拳")

    二、循环 while 循环 for循环 及关键字 break continue

      while循环:首先定义一个变量作为计数器,作为while循环的判断条件

      

    count = 0
    while count < 100:
        count += 1
        print(f"这是第{count}遍打印hello python")

      while 死循环的写法:while True:

    while True:
        print("hello python")

      关键字:break 终止循环

    count = 0
    while count < 100:
        count += 1
        print(f"这是第{count}遍打印hello python")
        if count == 50:
            break
        print(f"-------------这次打印{count}-----------")
    print("-----这是循环外的代码------")

      关键字:continue 中止当前循环,继续下一次循环

    count = 0
    while count < 100:
        count += 1
        print(f"这是第{count}遍打印hello python")
        if count == 50:
            continue
        print(f"-------------end:{count}-----------")
    print("-----这是循环外的代码------")

      利用while实现登录小程序:

    """
    利用while 循环实现登录小程序
    """
    user_info = {"user": "python26", "pwd": "lemonban"}
    while True:
        username = input("请输入账号:")
        password = input("请输入密码")
    
        if username == user_info['name'] 
                and password == user_info['pwd']:
            print("登录成功")
            break
        else:
            print("账号与密码不匹配")

    for 循环 :循环可以遍历可迭代对象 字符串、列表、字典

      语法:

      for i in xxx:

        # 循环体
     i 是一个临时变量
      
    li = [78, 32, 55, 77, 88, 90, 54, 67, 39]
    for i in li:
        if 0 < i < 60:
            print(f'你的成绩{i},不及格')
        elif i < 80:
            print(f'你的成绩{i},及格')
        else:
            print(f'你的成绩{i},优秀')

      for 循环的应用场景

      遍历列表:

      

    s = "sgsfagaf"
    for i in s:
        print(i)

      遍历元组

    tu = (1, 2, 3, 4, 56, 8, 7)
    for i in tu:
        print(i)

      遍历列表

    li = [1, 2, 3, 4, 6, 87, 6, 8, 4]
    for i in li:
    print(i)

      遍历字典

    dic = {'name': 'yuan', 'age': 18}
    # 直接遍历,便利的是字典的key
    for i in dic:
        print(i)
    # .values() 遍历字典中的value
    for i in dic.values():
        print(i)
    # .items() 同时遍历 key,value
    for k, v in dic.items():
        print(f'key:{k}')
        print(f'value:{v}')
     元组拆包
    a = 100
    b = 120
    a, b = 100, 200
    print(a)
    print(b)
    # 使用元组同时定义两个变量:元组拆包 元组中有几个元素对应变量
    c, d = (10, 20)
    print(c)
    print(d)
    tu = (11, 22, 33)
    aa, bb, cc = tu

      for 循环内置函数:range()

    # 累加 1 .。。100
    # 计算1+2+3.....100
    # 内置函数 range() 左闭右开
    # range()中传入一个参数:
    # range() 返回的数据是支持使用for进行遍历的,也能够进行下标取值和切片(返回的还是range类型的数据)
    print(list(range(10)))
    res = list(range(1, 101))
    print(res)
    print(list(range(1, 100, 10)))
    s = 0
    for i in range(1, 101):
        s += i
    print(s)


  • 相关阅读:
    poj 2262 筛法求素数(巧妙利用数组下标!)
    BestCoder Round #65 (ZYB's Premutation)
    BestCoder Round #65 (ZYB's Game)
    BestCoder Round #65 (ZYB's Biology)
    筛法求素数
    常见OJ提交结果对照表
    CSS3伸缩盒Flexible Box
    移动开发屏幕适配分析
    用PHP抓取页面并分析
    安装最新版本的PHPUnit后,不能使用
  • 原文地址:https://www.cnblogs.com/yuanxiaosong/p/12161210.html
Copyright © 2020-2023  润新知