运算符(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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值