运算符(and or not)的优先级运算与登录界面课后题优化版本

废话不多说,直接上程序

从最高优先级依次计算到最低优先级

# and or not
# print(2 > 1 and 1 < 4)
print(2 > 1 or 1 < 4 or 2 < 3 and 9 > 6 and 3 < 2)
# 优先级 ()> not > and > or
# 上面程序运行如下
# T or T or F
# T or F
# T
View Code

or 对数值的判断

# x or y 若x为非零,则返回x
print(1 or 2)
print(1 or 133)
print(0 or 2)
View Code

 因为只有非零数转换为bool值是T,0转换为bool值时是F。

演示:

print(bool(1))
print(bool(0))
View Code

 

and与or相反

即x and y,x为真,则返回y。

登录界面课后题优化版

account = "admin"
password = "admin"
i=0
while i <= 3:
    acc=input("请输入你的用户名")
    pas=input("请输入你的密码")
    if acc == account and pas == password :
       print("登陆成功")
       break
    else :
        i +=1
        if i == 1 :
            print("账号或密码错误,你还可以尝试2次")
        if i == 2 :
            print("账号或密码错误,你还可以尝试1次")
        if i == 3 :
            print("账号或密码错误,请明天再试")
            break
    continue
View Code
原文地址:https://www.cnblogs.com/zly9527/p/11197792.html