python这三个循环是不是同时进行,为什么当出现了a=false,循环还会进行下去,全输数yes

2025-03-04 19:55:37
推荐回答(2个)
回答1:

## 不修改题主的原始结构,帮修正了下代码,可以体会下break语句的含义。
## 有具体不明白的地方,可以再私信交流
def judgesecret(secret):
    #secret=input("请输入密码:")
    a = False
    print (secret)
    if 8<=len(secret)<=16:
        a = True
    if a:
        daxie = xiaoxie = shuzi = False
        #判断是否有大写,小写,数字
        for i in secret:
            if i.isupper():
                daxie = True 
                break # 如果有大写,跳出循环,下同
            #else:
            #    a=False
        for j in secret:
            if j.islower():
                xiaoxie =True
            #else:
            #    a=False 
        for k in secret:
            if k.isdigit():
                shuzi =True
            #else:
            #    a=False
        #同时满足三个条件才打印OK
        if (daxie and xiaoxie and shuzi):
            print("yes") 
        else:
            print ("no 不满足其他条件")
    else:
        print("no 位数不够")
    #else:
    #print("no") 

judgesecret("1234567")
judgesecret("1234567890")
judgesecret("1234567890RFabc")

回答2:

a=False也没有语句跳出循环,当然继续了,除非在每个循环后加个

if not a:break

跳出循环

def judgesecret(secret):    
    if not 8<=len(secret)<=16:
        return False
    upper=lower=digit=False
    for c in secret:
        if c.isupper():upper=True
        if c.islower():lower=True
        if c.isdigit():digit=True
    return upper and lower and digit
secret=input("请输入密码:")
if judgesecret(secret):
    print("yes")
else:
    print("no")