Python练习二

1.计算 1 - 2 + 3 ... + 99 中除了88以外所有数的总和

sum1 = 0
sum2 = 0
count = 0
while  count < 99:
    count += 1
    if count % 2 == 1:
        sum1 += count # 奇数合
    else:
        if count == 88:
            continue
        sum2 += count # 偶数合
print(sum1 - sum2)

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

username = "lin"
password = "110120"
s = 0
while s < 3:
    name = input("请输入您的姓名:") 
    pw = input("请输入您的密码:")
    if name == username and pw == password:
        print("登录成功")
        break
    else:
        if s < 2:
                res = input("账户或密码错误,你还有%d次机会,继续输入请直接回车,结束输入请输入no回车:" %(2-s))

        if res == "no":
            break
        else:
            s += 1
            if 3-s == 0:
                print("对不起,三次机会已全部用完")
            continue

 3.下面的结果是什么?

print(6 or 2 > 1)  # 6
print(3 or 2 > 1)  # 3
print(0 or 5 < 4)  # False
print(5 < 4 or 3)  # 3
print(2 > 1 or 6)  # True
print(3 and 2 > 1)  # True
print(0 and 3 > 1)  # 0
print(2 > 1 and 3)  # 3
print(3 > 1 and 0)  # 0
print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2)  # 2

 4.等待用户输入内容,检测用户输入内容中是否包含敏感字符?如果存在敏感字符提示“存在敏感字符请重新输入”,并允许用户重新输入并打印。敏感字符:“小粉拳”、“大铁锤”。

while True:
    content = input("请输入内容:")
    if "小粉拳" in content or "大铁锤" in content:
        print("存在敏感字符请重新输入:")
        continue
    else:
        print("您输入的内容是: %s" % content)
        break

并允许用户重新输入并打印。敏感字符:“小粉拳”、“大铁锤”。

原文地址:https://www.cnblogs.com/lin961234478/p/10318150.html