python循环语句相关练习答案

Posted on 2019-11-05 11:07  风吹碧霄啊  阅读(32)  评论(0)    收藏  举报
  1. 判断下列逻辑语句的True,False.
    1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

f or t or f and t and t or ff or t or f or f答案:ture
  1. 2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

     f and t or f and t and t or ff or f or f答案:false
  2. 求出下列逻辑语句的值。
    1),8 or 3 and 4 or 2 and 0 or 9 and 7 

    = 8 or (3 and 4) or (2 and 0) or (9 and 7)
    = 8 or 4 or 0 or 7
    = 8 or 0 or 7
    = 8 or 7
    = 8

    2),0 or 2 and 3 and 4 or 6 and 0 or 3

    = 0 or (2 and 3 and 4) or (6 and 0) or 3
    = 0 or (3 and 4) or 0 or 3
    = 0 or 4 or 0 or 3
    = 4 or 0 or 3
    = 4 or 3
    = 4

     

  3. 下列结果是什么?

     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 or 2 > 1
    = 6 or True
    = 6
    2)、3 or 2 > 1
    = 3 or True
    =3
    3)、0 or 5 < 4
    = 0 or False
    = False
    4)、5 < 4 or 3
    = False or 3
    =3
    5)、2 > 1 or 6
    =True or 6
    =True
    6)、3 and 2 > 1
    =3 and True
    =True
    7)、0 and 3 > 1
    =0 and True
    =0
    8)、2 > 1 and 3
    =True and 3
    =3
    9)、3 > 1 and 0
    =True and 0
    =0

     

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

    while 条件:
      循环体

     

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

    # while True:
    #     count = 66
    #     guess_number = int(input("请用户输入数字:"))
    #     if guess_number > count:
    #         print("结果大了")
    #     elif guess_number < count:
    #         print("结果小了")
    #     elif guess_number == count:
    #         print("结果正确")
    #         break

     

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

    times=1
    # while times<=3:
    #     count = 66
    #     guess_number = int(input("请用户输入数字:"))
    #     if guess_number > count:
    #         print("结果大了")
    #     elif guess_number < count:
    #         print("结果小了")
    #     elif guess_number == count:
    #         print("结果正确")
    #         break
    #     times += 1
    # else:
    #     print("太笨了")

     

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

    #方法1
    # count = 1
    # while count<11:
    #     if count != 7:
    #         print(count)
    #     count+=1
    #方法2
    # count = 1
    # while count<11:
    #     if count == 7:
    #         print(" ")
    #     else:
    #         print(count)
    #     count += 1
    #方法3
    # count = 1
    # while count<11:
    #     if count == 7:
    #         pass
    #     else:
    #         print(count)
    #     count += 1
    方法4
    count = 1
    while count<11:
      if count == 7:
          count+=1
      print(count)
      count += 1
    方法5  
      count = 0
    while count<10:
      count+=1
      if count == 7:
          continue
      print(count)

     

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

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

     

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

    count=1
    while count <=100:
      if count %2!=0:
          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 <=99:
      if count%2==0:
          sum -= count
      else:
          sum += count
      count+=1

     

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

    count=1
    while count <4:
      username = input("用户名")
      passsword = input("密码")
      if username == "tony" and passsword == "0825":
          print("登录成功")
      else:
          print("用户名或密码错误,还有%s次机会"%(3-count))
      count +=1

     

  13. 简述ASCII、Unicode、utf-8编码

    ASCII:只包含英文字母,数字,特殊字符。均占8位(1字节)
    Unicode:万国码,包括世界上所有的文字,不论中文、英文或符号,每个字符均占32位(4字节)。浪费空间,浪费资源。
    utf-8:万国码升级版,英文占8位(1字节)、欧洲字符占16位(2字节)、中文字符站24位(3字节)

     

  14. 简述位和字节的关系?

8位=1字节

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3