zoukankan      html  css  js  c++  java
  • python循环

     

    python_day_2

    一. 循环. while循环

    while 条件:
    代码块(循环体)
    执行流程:

    1. 判断条件是否为真. 如果真. 执行代码块
    2. 再次判断条件是否为真......
    3. 当条件为假.执行else 跳出循环. 循环结束

    1.让计算机连续打印5次corn,每次延迟一秒:

    import time
    count = 1
    while count <=5:
        print("corn")
        count = count + 1
        time.sleep(1)
    2.让用户尽情的喷,按 q 退出,并且过滤掉"马化腾"这三个字

    while True :
        s = input("请开始喷:")
        if s == 'q':
            break  #停止当前循环
    #过滤掉草泥马
        if "马化腾" in s : #in :在xxx中出现xx
            print("你输入的内容不合法,不能输出")
            continue  #停止当前本次循环,继续执行下一次循环
        print("喷的内容是"+ s)
    3.1+2+3+4+...+1000 = ?
    count = 1
    #准备一个变量
    sum = 0
    while count <=1000:
    #累加到sum
        sum = sum + count #把sum中的值(之前运算的结果)和当前的数相加
        count = count + 1
    print(sum)
    
    
    4.输出1-100的所有奇数?
    count = 1
    while count <=100:
        if count % 2 !=0:
            print(count)
        count = count + 1 

    二.格式化输出
    %s: 字符串的占位符, 可以放置任何内容(数字)
    %d: 数字的占位符

    name = input("请输入名字:")
    age = input("请输入你的年龄:")
    hobby = input("输入你的爱好:")
    gender = input("请输入你的性别:")
    
    #print(name + "今年" + age + "岁, 是一个老头, 爱好是" + hobby + ", 性别:" + gender)
    #% s: 表示字符串的占位符
    print("%s今年%s岁, 是一个老头, 爱好是%s, 性别:%s" % (name, age, hobby, gender))

    三. 运算符
    逻辑运算:
    and 并且的意思. 左右两端的值必须都是真. 运算结果才是真
    or 或者的意思. 左右两端有一个是真的. 结果就是真. 全部是假. 结果才能是假
    not 非的意思. 原来是假. 现在是真. 非真即假, 非假既真
    break 结束循环. 停止当前本层循环
    continue 结束当前本次循环. 继续执行下一次循环

    随堂作业:

     

    每个作业都是图一为运行结果图,图二为代码^-^

     

    一.判断下列列逻辑语句的True,False.
    # 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
    # 2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

    #1.True
    #2.False

    二.求出下列列逻辑语句的值。
    # 1),8 or 3 and 4 or 2 and 0 or 9 and 7
    # 2),0 or 2 and 3 and 4 or 6 and 0 or 3

    #1. 8
    #2. 4

    三.下列列结果是什么?
    # 1)、6 or 2 > 1
    # 2)、3 or 2 > 1
    # 3)、0 or 5 < 4
    # 4)、5 < 4 or 3
    # 5)、2 > 1 or 6
    # 6)、3 and 2 > 1
    # 7)、0 and 3 > 1
    # 8)、2 > 1 and 3
    # 9)、3 > 1 and 0
    # 10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2

    # 1)、6
    # 2)、3
    # 3)、False
    # 4)、3
    # 5)、true
    # 6)、true
    # 7)、0
    # 8)、3
    # 9)、0
    # 10)、2

    四.while循环语句基本结构?

    # while 条件:
    # 代码块(循环体)
    # 执行流程:
    # 1.首先判断条件是否为真,如果为真,执行循环体;
    # 2.然后继续判断条件是否为真...
    # 3.最后当天剑为假时,跳出循环体,循环结束
    五.利利⽤用while语句句写出猜⼤小的游戏:
    # 设定一个理想数字比如:66,让⽤用户输⼊入数字,如果⽐比66⼤大,
    # 则显示猜测 的结果⼤大了了;如果⽐比66⼩小,则显示猜测的结果⼩小了了;
    # 只有等于66,显示猜测结果正确,然后退出循环。
    count = 66
    while count:
        s = int (input("请输入一个数字:"))
        if s > count:
            print("猜大了")
        elif s < count:
            print("猜小了")
        else:
            print("猜对了")
            break
    七.使⽤用while循环输⼊ 1 2 3 4 5 6 8 9 10
    count = 1
    while count <= 10:
        
        if count != 7:
            print(count)
        count = count + 1 
    八.求1-100的所有数的和
    count = 1
    sum = 0
    while count <= 100:
        sum = sum + count
        count = count + 1
    print(sum)
    
    
    九.输出 1-100 内的所有奇数
    
    
    count = 1
    while count <= 100:
        if count % 2 != 0:
            print(count)
        count = count + 1
    十.输出 1-100 内的所有偶数
    
    
    count = 1
    while count <= 100:
        if count % 2 == 0:
            print(count)
        count = count + 1
    十一.求1-2+3-4+5......99的所有数的和
    count = 1
    sum = 0
    while count < 100:
    if count % 2 == 0:
    sum -= count
    else:
    sum += count
    count = count + 1
    print(sum)
    十二.用户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)

    account = 123456
    t = 3
    while account :
        s = int (input("请输入你的账号:"))
        t = t - 1
        if s != account:
            print("对不起,你的用户名不正确,请重新输入一遍(你的登陆次数还剩%s次)" % (t))
        else:
            print("恭喜你,登陆成功!")
        if t == 0 and s !=account:
            print("你的登录次数已用完,请明天再试")
            break
    十三.用户输⼊⼀个数. 判断这个数是否是⼀个质数(升级题)
    s = int(input("请输入一个数:"))
    if s <= 1:
        print("这不是质数")
    elif s == 2:
        print("这是质数")
    else:
        pn = 2
        while pn < s:
            if s % pn == 0:
                print("这不是质数")
                break
            pn += 1
        else:
            print("这是质数")
    #十四.输⼊⼀个广告标语. 判断这个广告是否合法. 根据最新的广
    # 告法来判断. 广告法内容过多. 我们就判断是否包含'最', '第⼀',
    # '稀缺', '国家级'等字样. 如果包含. 提示, ⼴告不合法
    # 例如: 1. ⽼男孩python世界第⼀. ==> 不合法
    # 2. 今年过年不收礼啊. 收礼只收脑⽩⾦. ==> 合法
      
    while True :
        s = input("请输入一个广告:")
        if ""in s or "第一"in s or "稀缺"in s or "国家级"in s:
            print("你输入的广告不合法,请重新输入")
        else:
            print("你输入的广告合法.")
    
    
    十四.输⼊⼀个数. 判断这个数是⼏位数(⽤算法实现)(升级题)

    num = int (input("请输入一个数:"))
    w = 0
    while num >= 1:
        num = num // 10
        w += 1
    print("这个数是%s位数" % (w))

     

    "继续奔跑 输掉一切也不要输掉微笑”

  • 相关阅读:
    今天,你ak了吗?①
    线段树模板
    DP(关于字符串,数字串的)
    Leedsdiscussion
    高数积分求旋转体体积
    tiny mission
    莫队+数组低级化的 优先队列
    LAB2
    Leedslecturepronouncation
    Eclipse Access Restriction
  • 原文地址:https://www.cnblogs.com/zycorn/p/9260535.html
Copyright © 2011-2022 走看看