zoukankan      html  css  js  c++  java
  • python while 格式化 运算符 编码

    #######################总结#############

    1. 循环

    while 条件:
    循环体(break, continue)
    循环的执行过程:
    执行到while的时候. 首先判断条件是否成立.如果成立. 执行循环体. 再一次判断条件.... 如果不成立. 直接跳出循环

    break 跳出当前本层循环
    continue 结束本次循环. 继续执行下一次循环

    2. 格式化输出

    %s 占位字符串(常用)

    %d 占位数字

    %f 占位浮点数

    name = input('你叫什么名字:')
    add = input('你来自哪里:')
    age =int(input('年龄:'))
    notlike=input('请输入你不喜欢的明星')
    # print("我叫"+name,'我来自'+add,'老婆'+wife,'不喜欢的明星:'+notlike)
    
    # print('我叫 %s,来自%s,年龄%d 不喜欢的明星%s' %(name,add,age,notlike ))
    #
    # print(f'我叫{name},来自{add},年龄{age} 不喜欢的明星{notlike}')

    #"{1} {0} {1}".format("hello", "world") # 设置指定位置

    3. 运算符

    !=
    += 累加 a += b  a = a + b

    and 并且, 左右两端同时为真. 结果才能是真
    or 或者, 左右两端有一个是真. 结果就是真
    not 非. 非真既假, 非假既真

    顺序: () => not => and => or

     x and y   x为真返回y  x为假返回x

     x  or  y   x为真返回真 x为假返回y

    x or y => if x == 0 then y else x
    and和or相反

    4. 编码

    1. ascii 8bit 1byte 包含了 英文, 数字, 特殊字符, 特殊操作符
    2. gbk 16bit 2byte 主要包含的: 中文, 日文, 韩文, 繁体中文
    3. unicode 32bit 4byte 万国码
    4. utf-8
    英文: 8bit 1byte
    欧洲: 16bit 2byte
    中文: 24bit 3byte

    ########################作业  ###################

    1、判断下列逻辑语句的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

    True

    False

    2、求出下列逻辑语句的值。
    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

    8

    4


    3、下列结果是什么?
    1)、6 or 2 > 1

    6


    2)、3 or 2 > 1

    3

    3)、0 or 5 < 4

    0

    4)、5 < 4 or 3

    3

    5)、2 > 1 or 6

    1

    6)、3 and 2 > 1

    1

    7)、0 and 3 > 1

    0

    8)、2 > 1 and 3

    3

    9)、3 > 1 and 0

    0

    10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2

    2

    4、while循环语句基本结构?
    while 条件:
    循环体(break, continue)


    5、利⽤while语句写出猜⼤⼩的游戏:
    设定⼀个理想数字⽐如:66,让⽤户输⼊数字,如果⽐66⼤,则显示猜测
    的结果⼤了;如果⽐66⼩,则显示猜测的结果⼩了;只有等于66,显示猜测结果
    正确,然后退出循环。

    while True:
        content =int(input('请输入数字'))
        if content > 66:
            print('你输入的结果大了')
        elif content < 66:
            print('你输入的结果小了')
        else:
            print('你才猜了')
            break

    6、在5题的基础上进行升级:
    给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循
    环,如果三次之内没有猜测正确,则⾃动退出循环,并显示‘太笨了你....’。

    number=66
    count=3
    while True:
        content =int(input('请输入数字'))
        if content > number:
            print('你输入的结果大了')
        elif content < number:
            print('你输入的结果小了')
        else:
            print('你才猜了')
        count -= 1
        if count == 0:
            print('你太笨了')
            break
    
    
    number=66
    count=1
    while  count<=3:
        content =int(input('请输入数字'))
        if content > number:
            print('你输入的结果大了')
            count+=1
            continue
        elif content < number:
            print('你输入的结果小了')
            count += 1
            continue
        else:
            print('你猜对了')
            count += 1
            continue
    print("已经 猜完了3次")

    7.使用while循环输入1 2 3 4 5 6 8 9 10

    number =0
    while number < 10:
        number+=1
        print(number)


    8.求1-100的所有数的和

    sum = 0
    number =1
    while number <= 100:
        sum=sum+number
        number=number+1
    print(sum)


    9.输出 1-100 内的所有奇数

    count = 1
    while count <= 100:
        if count % 2 == 1:
            print(count)
        count +=1


    10.输出 1-100 内的所有偶数

    count = 1
    while count <= 100:
        if count % 2 == 0:
            print(count)
        count += 1


    11.求1-2+3-4+5 ... 99的所有数的和.

    count = 1
    sum = 0
    while count < 100:
    if count %2 == 0:
    sum -= count
    #-2 -6 -12 -20 30
    else:
    sum += count
    #+1 +3 +5
    count += 1
    print(sum)

    12.用户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤
    字符串格式化)

    count=0
    number=3
    while  number:
        number=number-1
        user=input('请输入用户名:')
        password=input('请输入密码:')
        if user=='admin' and password=='123456':
            print('输入正确')
        else:
            print('用户名或密码错误,你还剩余%s 次' %(number))

    13. 用户输⼊⼀个数. 判断这个数是否是⼀个质数(升级题).
    2 3 5 7 11 13

    1 2
    1 3
    1 5
    1 7
    1 11
    1 13

    num = int(input('请输入一个数字:'))
    if num <= 1:
    print('这不是质数')
    elif num == 2:
    print('这是一个质数!')
    else:
    i=2
    while i < num:
    if num%i == 0:
    print('这不是一个质数')
    break
    i += 1
    else:
    print ('这是一个质数!')
    #方法二
    n=int(input('请输入一个数字:'))
    for i in range(2,n):
    if n %i ==0:
    print('不是质数')
    break #有了break就不会执行后面的else
    else:
    print('是一个质数')

    14. 输个告标语. 判断这个公告是否合法. 根据最新的公告法来判断. ⼴
    告法内容过多. 我们就判断是否包含'最', '第⼀', '稀缺', '国家级'等字样. 如果包
    含. 提示, 广告不合法
    例如, 1. ⽼男孩python世界第⼀. ==> 不合法
        2. 今年过年不收礼啊. 收礼只收脑⽩⾦. ==> 合法

    content = input('请输入')
    if ''in content or '第⼀'in content or '稀缺' in content or '国家级'in content:
        print('⽼男孩python世界第⼀')
    else:
        print('今年过年不收礼啊. 收礼只收脑')

    14. 输入数字. 判断这个数是几位数(⽤算法实现)(升级题)

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

    明⽇默写代码:
    1. 求1-100之间所有的数的和

    number=0
    sum=0
    while number < 100:
    number+=1
    sum = number + sum
    print(sum)

    2. And or not的含义和特征

    and 变量 a 和 b 都为 true 则为真
    or 变量 a 和 b 都为 true,或其中一个变量为 true
    not 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。

    3. break continue的含义. 有什么区别

    #continue 停止当前本次循环,继续执行下一次循环
    #break 彻底干掉一个循环

    不怕大牛比自己牛,就怕大牛比自己更努力
  • 相关阅读:
    Writing Custom Providers
    terraform 几个方便的工具
    几张简单的terraform flow 图——可以快速了解terraform的使用
    Stateful Kubernetes Applications Made Easier: PSO and FlashBlade
    使用k8s && minio 进行 postgres 数据库自动备份
    Understanding how uid and gid work in Docker containers
    nightwatchjs 基于nodejs&& webdriver 协议的自动化测试&&持续集成框架
    hasura graphql-engine graphql2chartjs 方便的graphql 转换chartjs 的类库
    nginx unit 1.8 支持基于java servlet 的开发模型
    试用 openresty/lua-resty-shell
  • 原文地址:https://www.cnblogs.com/zaizai1573/p/day-2.html
Copyright © 2011-2022 走看看