练习

  1. 判断下列逻辑语句的结果,一定要自己先分析

    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

    验证:

    print(1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
    print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
  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

    3)1 and 0 or 8 and 9 and 5 or 2

    4)4 or 8 and not False and 8 or 9

    答案:

    1)8

    2)4

    3)5

    4)4

    验证:

    print(8 or 3 and 4 or 2 and 0 or 9 and 7)
    print(0 or 2 and 3 and 4 or 6 and 0 or 3)
    print(1 and 0 or 8 and 9 and 5 or 2)
    print(4 or 8 and not False and 8 or 9)
  3. 下列结果是什么? (2>1这种是一体)

    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

    验证:

    print(6 or 2 > 1)
    print(3 or 2 > 1)
    print(0 or 5 < 4)
    print(5 < 4 or 3)
    print(2 > 1 or 6)
    print(3 and 2 > 1)
    print(0 and 3 > 1)
    print(2 > 1 and 3)
    print(3 > 1 and 0)
    print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2)
  4. 简述ASCII、Unicode、utf-8编码

    # ASCII:美国人发明的,不支持中文 英文8位1字节
    # Unicode:万国码 英文16位2个字节 中文32位4个字节
    # utf-8:(可变编码)英文8位1个字节 欧洲16位2个字节 亚洲24位3个字节
  5. 简述位和字节的关系?

    # 1字节=8位
    # 1Bytes=1bits
  6. while循环语句基本结构?

    while 空格 条件 冒号
    缩进 循环体
    while 空格 条件 冒号
    缩进 循环体
    else 冒号
    缩进 结果
  7. 利用while语句写出猜大小的游戏:

    设定一个理想数字比如:66,让用户输入数字,

    如果比66大,则显示猜测的结果大了;

    如果比66小,则显示猜测的结果小了;

    只有等于66,显示猜测结果正确,然后退出循环。

    代码:

    num=66
    while True:
    num1=int(input("请输入猜测数字"))
    if num1==num:
    print("猜测正确!")
    break
    elif num1>num:
    print("猜大了")
    else:
    print("猜小了")
  8. 在7题的基础上进行升级:

    给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,

    如果三次之内没有猜测正确,则自动退出循环,并显示‘太笨了你....’。

    代码:

    num=66
    n=3
    while n:
    num1=int(input("请输入猜测数字"))
    n-=1
    if num1==num:
    print("猜测正确!")
    break
    elif num1>num:
    print("猜大了")
    else:
    print("猜小了")
    else:
    print("你太笨了",end="")
  9. 使用while循环输出 1 2 3 4 5 6 8 9 10

    num=0
    while num<10:
    num += 1
    if num==7:
    continue
    print(num)
  10. 求1-100的所有数的和

    答案:5050

    num=0
    n=1
    while n<=100:
    num=num+n
    n=n+1
    print(num)
```python

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

    n=1
    while n<100:
    print(n)
    n=n+2
  2. 输出 1-100 内的所有偶数

    n=0
    while n<100:
    n=n+2
    print(n)
  3. 求1-2+3-4+5 ... 99的所有数的和

    答案:50

    代码:

    num=0
    n=1
    while n<100:
    if n%2==0:
    num=num-n
    else:
    num=num+n
    n=n+1
    print(num)
  4. ⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)

    name="alex"
    psd="admin123" n=3
    while n:
    n=n-1
    num1=input("请输入账号:")
    psd1=input("请输入密码:")
    if num1==name and psd==psd1:
    print("成功登陆!")
    break
    else:
    print("账号或密码错误,你还剩下%d次机会"%(n))
  5. 求1-3+5-7+9-...99的结果

    sum = 0
    flag = 1
    num = 1
    while num < 100:
    sum += num * flag
    num = num + 2
    flag = flag * -1
    print(sum)

while与格式化的练习的更多相关文章

  1. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  2. 理解CSS视觉格式化

    前面的话   CSS视觉格式化这个词可能比较陌生,但说起盒模型可能就恍然大悟了.实际上,盒模型只是CSS视觉格式化的一部分.视觉格式化分为块级和行内两种处理方式.理解视觉格式化,可以确定得到的效果是应 ...

  3. ASP.NET Aries 入门开发教程6:列表数据表格的格式化处理及行内编辑

    前言: 为了赶进度,周末也写文了! 前几篇讲完查询框和工具栏,这节讲表格数据相关的操作. 先看一下列表: 接下来我们有很多事情可以做. 1:格式化 - 键值的翻译 对于“启用”列,已经配置了格式化 # ...

  4. C# DateTime日期格式化

    在C#中DateTime是一个包含日期.时间的类型,此类型通过ToString()转换为字符串时,可根据传入给Tostring()的参数转换为多种字符串格式. 目录 1. 分类 2. 制式类型 3. ...

  5. 在Sublime Text 3上安装代码格式化插件CodeFormatter

    1.了解CodeFormatter插件 在Sublime Text 3中编写代码,为了能让我们的代码格式变得漂亮整洁,需要一个能自动格式代码的插件.这里发现CodeFormatter插件不错,它能支持 ...

  6. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  7. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  8. js格式化日期

    /** *对日期进行格式化, * @param date 要格式化的日期 * @param format 进行格式化的模式字符串 * 支持的模式字母有: * y:年, * M:年中的月份(1-12), ...

  9. fmt标签把时间戳格式化日期

    jsp页面标签格式化日期 <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f" %> ...

  10. Oracle ------ SQLDeveloper中SQL语句格式化快捷键

    Oracle SQL Developer中SQL语句格式化快捷键: 每次sql复制到SQL Developer面板的时候,格式老不对,而且看起来很不舒服,所有的sql都挤在一行完成. 这时我们可以全选 ...

随机推荐

  1. 【leetcode】Sliding Puzzle

    题目如下: On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square ...

  2. UFUN函数 UF_TRNS(平移 变换)( uf5943 , uf5947)

    //设置class_dialog选择过滤 static int init_proc(UF_UI_selection_p_t select,void* user_data) { ; //实体 片体 UF ...

  3. sh_10_嵌套打印小星星

    sh_10_嵌套打印小星星 # 需求 # # 在控制台连续输出五行 *,每一行星号的数量依次递增 # * # ** # *** # **** # ***** # 开发步骤 # # 1> 完成 5 ...

  4. sh_05_偶数求和

    sh_05_偶数求和 # 计算 0 ~ 100 之间 所有 偶数 的累计求和结果 # 开发步骤 # # 1. 编写循环 确认 要计算的数字 # 2. 添加 结果 变量,在循环内部 处理计算结果 # 1 ...

  5. 容器适配器————heap

    堆(heaps)不是容器,而是一种特别的数据组织方式.堆一般用来保存序列容器. 堆是一个完全二叉树,每个节点与其子节点位置相对.父节点总是大于或等于子节点,这种情况下被叫作大顶堆,或者父节点总是小于或 ...

  6. 箭头函数详解()=>{}

    摘要:箭头函数有几个使用注意点. (1)函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象,箭头函数继承而来的this指向永远不变. (2)不可以当作构造函数,也就是说,不可以使用n ...

  7. Ajax监测开始执行及结束执行

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  8. Using FileUpload

    Using FileUpload FileUpload can be used in a number of different ways, depending upon the requiremen ...

  9. shell脚本之结构化命令if...then...fi

    if的用法日常主要用于数值或者字符串的比较来实现结构化的,模拟人脑,就是如果遇到什么事情,我们应该做什么 语法格式分为 1. if command;then command;fi    (如果if满足 ...

  10. leetcode-mid-others-169. Majority Element¶

    mycode  54.93% class Solution(object): def majorityElement(self, nums): """ :type num ...