Python -- while练习题

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

    n = 66
    while True:
        num = int(input("输入一个数字:"))
        if num > n:
        	print("猜测的结果大了")
        elif num < n:
            print("猜测的结果小了")
        else:
            print("猜测正确")
            break
    
  2. 在上一题的基础,设置:给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则自动退出循环,并显示‘大笨蛋’

    n = 66
    count = 1
    while count <= 3:
        num = int(input("输入一个数字:"))
        if num > n:
        	print("猜测的结果大了")
        elif num < n:
            print("猜测的结果小了")
        else:
            print("猜测正确")
            break
        count = count + 1
    else:
        print("大笨蛋")
    
  3. 判断下列逻辑语句的True,False

  • 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

    ​ True

  • not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

    ​ False

  1. 求出下列逻辑语句的值。
  • 8 or 3 and 4 or 2 and 0 or 9 and 7

    ​ 8

  • 0 or 2 and 3 and 4 or 6 and 0 or 3

    ​ 4

  1. 下列结果是什么?
  • 6 or 2 > 1 6

  • 3 or 2 > 1 3

  • 0 or 5 < 4 False

  • 5 < 4 or 3 3

  • 2 > 1 or 6 True

  • 3 and 2 > 1 True

  • 0 and 3 > 1 0

  • 2 > 1 and 3 3

  • 3 > 1 and 0 0

  • 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2

    ​ 2

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

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

    count = 1
    sum = 0
    while count <= 100:
        sum = sum + count
        count = count + 1
    print(sum)
    
  3. 输出 1-100 内的所有奇数 (奇数就是除以2余数不为0)

    num = 0
    count = 1
    while count <= 100:
        num = num + 1
        if num % 2 == 1:
            print(num)
        count = count + 1
    
  4. 输出 1-100 内的所有偶数 (偶数就是除以2余数不为1)

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

    sum = 0
    count = 1
    while count <= 99:
        if count % 2 == 1:
            sum = sum + count
        elif count % 2 == 0:
            sum = sum - count
        count = count + 1
    print(sum)
    
  6. 简述ASCII、Unicode、utf-8编码英文和中文都是用几个字节?

    ASCII 英文 1 个字节, 不支持中文

    UNICODE 英文 2 个字节, 中文 4 个字节

    UTF-8 英文 1 个字节, 中文 3 个字节

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

    1字节 = 8bit

  8. "老男孩"使用GBK占几个字节,使用Unicode占用几个字节?

    GBK 占用 6 个字节 UNICODE 占用 12 字节

  9. 猜年龄游戏升级版 要求:允许用户最多尝试3次,每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y,就继续让其猜3次,以此往复,如果回答N,就退出程序,如何猜对了,就直接退出。

    count = 1
    while count <= 3:
        age = int(input("请输入年龄:"))
        if age > 18:
            print("猜大了")
        elif age < 18:
            print("猜小了")
        else:
            print("猜测正确")
            break
        count = count + 1
        if count > 3:
            choice = input("是否还想继续玩? Y or N")
            if choice == "Y":
                count = 1
            else:
                break
    
  10. ⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)

    count = 1
    while count <= 3:
        username = input("用户名")
        password = input("密码")
        if username == "Agoni" and password == "123":
            print("登陆成功")
            break
        else:
            print("用户名或密码错误")
        print("当前输入%s次,剩余%s次" % (count, 3 - count))
        count = count + 1
    
原文地址:https://www.cnblogs.com/Agoni-7/p/11006347.html