简体   繁体   English

为什么我的for循环不检查每个字符?

[英]Why does my for loop not check every character?

So I'm a bit of a beginner at python and I cant for the life of me figure out why it wont check and add on for every letter 所以我是python的新手,我一辈子都无法弄清楚为什么它不会检查并添加每个字母

def howstrong (password):
    points = len(password)
    charactersallowed = ["!", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+"]

    for ch in password:
        if ch.isupper():
            points= points+5
        elif ch.islower():
            points = points+5
        elif ch.isdigit():
            points = points+5
        elif ch in charactersallowed:
            points= points+5
        else:
            points = points+0
        return points

If I input a password of Password! 如果我输入密码为密码! into my code it tells me my points are 14 but for this password it should be 24? 在我的代码中,它告诉我我的积分是14,但是对于此密码,它应该是24? Below I will add the rest of my code but I doubt that its in that part and I believe that there's an error in my for loop somewhere. 在下面,我将添加其余的代码,但我对此部分表示怀疑,并且我认为我的for循环中某处存在错误。

def checkingmain():

    while 1:
        p = input("\nEnter password: ")
        if len(p) < 8 or len(p) > 24:
            print("Password must be 6 to 12 characters.")
        elif input("Re-enter password: ") != p:
            print("Passwords did not match. Please try again.")

        else:
            score= howstrong(p)
            if not score:
                print("Invalid character detected. Please try again.")

            else:
                if score <= 0:
                    print("your password is weak",score)
                elif score>0 and score<20:
                    print("your password is medium",score)
                else:
                    print("your password is strong",score)
            break

I'd appreciate if someone would get back to me with an understandable solution for a somewhat python beginner. 如果有人能为某个python初学者提供一个可理解的解决方案,我将不胜感激。

It only ever checks the first character, because you return inside the loop. 它只检查第一个字符,因为您在循环内返回。 Move your return statement back one indent, so it is not inside the for loop. 将return语句移回一个缩进,因此它不在for循环内。

Since you have your return statement inside the loop, it only runs the loop one time before returning from the function. 由于您在循环内有return语句,因此它仅在从函数返回之前运行循环一次。 If you move your return statement back one tab it should work 如果将return语句移回一个选项卡,它应该可以工作

The return statement is inside the for loop , so when your program reaches the end of the for loop for the first time, it simply returns from the function, so your for loop is terminated. return语句位于for循环内,因此,当您的程序第一次到达for循环的末尾时,它只是从函数中返回,因此for循环终止。 A little change in your code like shown below will help you. 如下所示,对代码进行一些小的更改将对您有所帮助。

for ch in password:
    if ch.isupper():
        points= points+5
    elif ch.islower():
        points = points+5
    elif ch.isdigit():
        points = points+5
    elif ch in charactersallowed:
        points= points+5
    else:
        points = points+0
return points

Hope it helps ! 希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的正则表达式会为字符串中的每个字符返回元组? - Why does my regular expression return tuples for every character in a string? 为什么我的程序只删除列表中的所有其他字符? - Why does my program only delete every other character in a list? 为什么我的函数检查一个字母是否在一个字符串中无限循环? - Why does my function to check whether a letter is in a string loop infinitely? 为什么我的 for 循环只检查最后一项? - Why does my for loop only check the last item? 为什么我的打字机打印的每个字符都换行? - Why does my typer's print make a new line for every character? 使用While For循环的简单电梯程序Python。 为什么循环不更新列表中的每个项目? - Simple Elevator Program Python Using While For Loop. Why does my loop not update every item on list? Python是否在每个循环中执行我的方法? - Does Python execute my method every loop? 为什么我的For循环将每个字符串相乘 - Why is my For Loop multiplying every String 为什么我的计数器不更新,即使我在每个循环中都添加了一个? - Why does my counter not update even though I am adding one on every loop? 为什么我的 python 循环每次运行都会给出不同的数组,以及如何让它每次都产生相同的结果? - Why does my python loop give a different array every run, and how to make it produce the same result everytime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM