运算符和while循环题

第二天作业

第一题:1.判断下列逻辑语句的结果,一定要自己先分析 (3<4这种是一体)

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
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
    

第三题:下列结果是什么? (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
  • 答案:

  • 第一小题:6
    第二小题:3
    第三小题:False
    第四小题:3
    第五小题:True
    第六小题:True
    第七小题:0
    第八小题:3
    第九小题:0
    第十小题:2
    

第四题:简述ASCII、Unicode、utf-8编码

  • 答案:

  • ascii
         定义:将英文字母和常用符号用特定的数字去表达,但不支持中文
         比如:a 一个字符占用8位
     unicode
         定义:包含所有字符的字符集,解决了世界各地每个地方所用的编码方式不一样的尴尬,让我们更好沟通。
         比如:英文  4个字节  32位
              中文  4个字节  32位
     utf-8 (最流行的编码集)
         定义:为了解决Unicode存在内存问题,将Unicode再次编码的一种编码方式
         比如:英文 1字节      8位
              欧洲 2字节      16位
              亚洲 3字节      24位
    
    

第五题:简述位和字节的关系?

  • 答案:

  • 位: 二进制位(bit)是计算机存储信息的基本单位。
    字节:8个连续的二进制位为一个字节。
    转换关系如下:
    8bit = 1byte
    1024byte = 1KB
    1024KB = 1MB
    1024MB = 1GB
    1024GB = 1TB
    1024TB = 1PB
    1024TB = 1EB
    1024EB = 1ZB
    1024ZB = 1YB
    1024YB = 1NB
    1024NB = 1DB
    常⽤到TB就够了
    

第六题:while循环语句基本结构?

  • 答案:

  • 1.单while基本结构:
    
    while 条件:
          循环体
          
    2.while else 结构:
    
    while 条件:
          循环体
    else:
        结果
    

第七题:利用while语句写出猜大小的游戏:

设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测的结果大了;如果比66小,则显示猜测的结果小了;只有等于66,显示猜测结果正确,然后退出循环。

  • 答案:

  • num  = int(input("请输入一个数字:"))
    while num != 66:
        if num > 66:
            print("你猜的数字太大了!")
        else:
            print("你猜的数字好小啊!")
    else:
        print("恭喜你,猜对啦正确结果")
    

第八题:在7题的基础上进行升级:

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

  • 答案:

  • print("我给您三次猜测的机会!")
    count = 3
    while count > 0:
        count -= 1
        num = int(input("请输入你猜测的数字:"))
        if num > 66:
            print("你猜的数字太大了!")
        elif num < 66:
            print("你猜的数字好小啊!")
        else:
            print("恭喜你,猜对啦正确结果")
            break
    else:
        print("你太笨啦吧!")
    

第九题:使用while循环输出 1 2 3 4 5 6 8 9 10

  • 答案:

  • count = 0
    while 0 <= count <= 9:
        count += 1
        print(count)
    
    

第十题:求1-100的所有数的和

  • 答案:

  • count = 0
    num = 0
    while 0 <= count <= 100:
        num += count
        count += 1
    print("1-100的所有数的和是%s" % num)
    
    

第十一题:输出 1-100 内的所有奇数

  • 答案:

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

第十二题:输出 1-100 内的所有偶数

  • 答案:

  • count = 2
    while 2 <= count <= 100:
        print(count)
        count += 2
    
    

第十三题:求1-2+3-4+5 ... 99的所有数的和

  • 答案:

  • count = 1
    sum = 0
    while count <= 99:
        if count % 2 == 1:
            sum += count
        else:
            sum -= count
        count += 1
    print(sum)
    
    

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

  • 答案:

  • count = 3
    while count > 0:
      name = input("请输入用户名:")
        password = input("请输入密码:")
        if "顾浩浩" == name and "123456..." == password:
            print("登陆成功!")
            break
        else:
            count -= 1
            if "小浩" == name:
                if count != 0:
                    print("密码错误请重新输入!您还有%s次机会" % count)
            else:
                if count != 0:
                    print("账号错误请重新输入!您还有%s次机会" % count)
    else:
        print("你个大笨蛋,连账号密码都记不住!")
    
    
    
posted @ 2019-09-06 19:07  小皮浩  阅读(440)  评论(1编辑  收藏  举报