Python怎么循环调用这个函数

Python怎么循环调用这个函数

问题描述:

这整个函数怎么循环调用呀

def check_number_exist(password_str):
for c in password_str:
if c.isnumeric():
return True
return False
def check_alpha_exist(password_str):
for c in password_str:
if c.isalpha():
return True
return False
def check_space_exist(password_str):
for c in password_str:
if c.isspace():
return False
return True

def main():
password = input('请输入密码')
strength_level = 0
if len(password) >= 8:
strength_level += 1
else:
print('密码长度要求至少8位数!')
if check_number_exist(password):
strength_level += 1
else:
print('密码要求包含数字!')
if check_alpha_exist(password):
strength_level += 1
else:
print('密码要求包含字母!')
if check_space_exist(password):
strength_level += 1
else:
print('密码不要求包含空格!')
if strength_level == 4:
print('恭喜!密码强度合格!')
else:
print('密码强度不合格!')

if name == 'main':
main()

什么循环调用,没明白?
是这样吗

def check_number_exist(password_str):
    for c in password_str:
        if c.isnumeric():
            return True
    return False
def check_alpha_exist(password_str):
    for c in password_str:
        if c.isalpha():
           return True
    return False
def check_space_exist(password_str):
    for c in password_str:
        if c.isspace():
            return False
    return True

def main():
    while True:  #加个循环
        password = input('请输入密码')
        strength_level = 0
        if len(password) >= 8:
            strength_level += 1
        else:
            print('密码长度要求至少8位数!')
        if check_number_exist(password):
            strength_level += 1
        else:
            print('密码要求包含数字!')
        if check_alpha_exist(password):
            strength_level += 1
        else:
            print('密码要求包含字母!')
        if check_space_exist(password):
            strength_level += 1
        else:
            print('密码不要求包含空格!')
        if strength_level == 4:
            print('恭喜!密码强度合格!')
            break #密码强度合格就跳出循环
        else:
            print('密码强度不合格!请重新输入')

if __name__ == "__main__":
    main()

img

将main函数改写成如下,即可循环调用,并且当密码强度符合要求时程序退出:

def main():
    while True:
        password = input('请输入密码')
        strength_level = 0
        if len(password) >= 8:
            strength_level += 1
        else:
            print('密码长度要求至少8位数!')
        if check_number_exist(password):
            strength_level += 1
        else:
            print('密码要求包含数字!')
        if check_alpha_exist(password):
            strength_level += 1
        else:
            print('密码要求包含字母!')
        if check_space_exist(password):
            strength_level += 1
        else:
            print('密码不要求包含空格!')
        if strength_level == 4:
            print('恭喜!密码强度合格!')
            break
        else:
            print('密码强度不合格!')


如果对你有帮助,请点击采纳按钮。