python之道02

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

    答案:

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

    答案:

    num = 66
    count = 0
    while count < 3:
        my_input = int(input('输入数字: '))
        if my_input > num:
            print('大了')
        elif my_input < num:
            print('小了')
        else:
            print('正确')
            break
        count += 1
        if count == 3:
            print('大笨蛋')
            break
            
     # 方法二好(小升级)       
            
    num = 66
    count = 0
    while count < 3:
        my_input = int(input('输入数字: '))
        if my_input > num:
            print('大了')
        elif my_input < num:
            print('小了')
        else:
            print('正确')
            break
        count += 1
    else: # 如果三次之内没有猜测争取,则自动退出并显示'大笨蛋'
      	print('大笨蛋')
        break
    
  3. 判断下列逻辑语句的True,False

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

    答案:

    优先级:
    算数运算符高于逻辑运算符
    逻辑运算符优先级从高到低 not and or 
    True
    False
    
  4. 求出下列逻辑语句的值。

    • 8 or 3 and 4 or 2 and 0 or 9 and 7
    • 0 or 2 and 3 and 4 or 6 and 0 or 3

    答案:

    8
    4
    
  5. 下列结果是什么?

    • 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

  6. 使用while循环输出 1 2 3 4 5 6 8 9 10 # 这里注意看题没有7

答案:

# 方法一:
count = 0
while count < 11:
  	if count != 7: # 如果count不等于7就打印
      	print(count)
    count += 1
# 方法二:
count = 1
while count < 11:
  if count == 7: # 如果count等于7 就给count加个1再打印,然后count继续+1循环打印
    	count += 1
  print(count)
  count += 1
 # 方法三:
count = 0
while count < 10:
    count += 1
    if count == 7:
        continue # 如果count等于7, 跳出本次循环,继续下次循环
    print(count)
  1. 求1-100的所有数的和

答案:

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

答案:

count = 0
while count < 101:
    if count % 2 != 0:
        print(count)
    count +=1 
# 个人见解 
# 除2余数不为0
  1. 输出 1-100 内的所有偶数 (偶数就是除以2余数不为1)

答案:

count = 0
while count < 101:
    if count % 2 != 1:
        print(count)
    count +=1
# 除2余数不为1
  1. 求1-2+3-4+5 ... 99的所有数的和

答案:

x = 1
sum = 0
while x < 100:
    if x % 2 == 0:
        sum -= x  # sum = sum - x
    else:
        sum += x
    x += 1
print(sum)
# 输出结果
50
# 个人见解
# x是从1到99的数,当取值100的时候while循环条件不满足,不再执行.关键条件:当x为偶数也就是取余为0的时候,sum的值就等于

#################################################################
# 原始方法:
count = 1
num_sum = 0  # 偶数的减和
sum_num = 0  # 奇数的加和
while count < 100:
    if count % 2 == 0:
        num_sum = num_sum - count
    else:
        sum_num = sum_num + count
    count +=1

print(num_sum + sum_num)

######################################################

# 方法二:
count = 1
num_sum = 0  # 总和
while count < 100:
    if count % 2 == 0:
        num_sum = num_sum - count
    else:
        num_sum = num_sum + count
    count +=1

print(num_sum)

# 规律就是偶数是减,用num_sum = num_sum - count,奇数是加num_sum = num_sum + count

  1. 简述ASCII、Unicode、utf-8编码英文和中文都是用几个字节?

答案:

ASCII:英文字符 英文1个字节 不支持中文
Unicode:万国码 英文2个字节 中文4个字节
utf-8: 万国码的升级版本 英文1个字节 欧洲2个字节 亚洲3个字节

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

答案:

1byte = 8bit

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

答案:

GBK中文2个字节:所以'老男孩'共占6个字节
Unicode中文4个字节:所以'老男孩'共占12个字节

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

答案:

num = 66
count = 0
while count < 3:
    my_input = int(input('输入数字: '))
    if my_input > num:
        print('大了')
    elif my_input < num:
        print('小了')
    else:
        print('正确')
    count += 1
    if count == 3:
        my_input1 = input('用户是否还想继续玩,Y或N: ').strip()
        if my_input1.upper() == 'Y':
            count = 0
            continue
        elif my_input1.upper() == 'N':
            break
        
# 方式二:
count = 1
age = 66
while count < 4:
    age_input = int(input("请输入年龄:"))
    if age_input > age:
        print("大了")
    elif age_input < age:
        print("小了")
    else:
        print("猜对了")
        break

    if count == 3:
        choice = input("是否继续(Y/N):")
        if choice == "Y":
            count = 0
        else:
            break

    count += 1

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

答案:

count = 3
while count < 4:
    count -= 1
    username = input('Name: ').strip()
    passwd = input('Password: ').strip()
    if username == 'admin' and passwd == '123':
        print('成功')
        break
    else:
        print(f'失败,剩余{count}次')
        if count == 0:
            break
# 老师方法:
count = 3

while count > 0:
    user = input("user:")
    pwd = input("password:")
    if user == 'alex' and pwd == 'alex3714':
        print("登录成功!")
        break
    else:
        print(f"你是Alex吗?这都记不住!剩余次数{count - 1}")
    count -= 1

原文地址:https://www.cnblogs.com/zanao/p/10979604.html