简体   繁体   English

如何确保我只打印一次?

[英]How to make sure I only print once?

I am a beginner to python, I am trying to create a program that check's the password variability.我是 python 的初学者,我正在尝试创建一个程序来检查密码可变性。

The code is finished but when I execute it, it prints the same message multiple times.代码已完成,但是当我执行它时,它会多次打印相同的消息。 But I only want it to print once.但我只希望它打印一次。

def controleer_paswoord(wachtwoord):
    import re
    for item in wachtwoord:
        kleine_letter =  re.search( "[a-z]",wachtwoord)
        hoofd_letter = re.search( "[A-Z]",wachtwoord)
        cijfer = re.search ("[0-9]",wachtwoord)
        karakter = re.search ("[$#@]",wachtwoord)
        lengte = (len(wachtwoord)<6 or len(wachtwoord)>12)
        if kleine_letter == None:
            print("{0} -> Geldig paswoord?  False".format(wachtwoord))
        elif hoofd_letter == None:
            print("{0} -> Geldig paswoord?  False".format(wachtwoord))
        elif cijfer == None:
            print("{0} -> Geldig paswoord?  False".format(wachtwoord))
        elif karakter == None:
            print("{0} -> Geldig paswoord?  False".format(wachtwoord))
        elif lengte == None:
            print("{0} -> Geldig paswoord?  False".format(wachtwoord))
        else:
            print("{0} -> Geldig paswoord?  True".format(wachtwoord))

# wachtwoord = (input("Geef hier uw gewenste wachtwoord in:" ))
wachtwoord = "2w3E"
controleer_paswoord(wachtwoord)

what I get:我得到了什么:

#2w3E -> Geldig paswoord?  False
#2w3E -> Geldig paswoord?  False
#2w3E -> Geldig paswoord?  False
#2w3E -> Geldig paswoord?  False

what I want:我想要的是:

#2w3E -> Geldig paswoord?  False

At for item in wachtwoord: you are iterating over each character in the string, which seems to be unintended.for item in wachtwoord:您正在迭代字符串中的每个字符,这似乎是无意的。 It dosen't seem like you use item anywhere inside the for loop, so it just repeats the process the same way for each character.您似乎没有在 for 循环内的任何地方使用item ,因此它只是对每个字符以相同的方式重复该过程。 Try removing that line尝试删除该行

You print for every character in the password.您打印密码中的每个字符。 Remove that loop.删除那个循环。 The re searches go through the entire password, so you don't need to loop through the characters.重新搜索会遍历整个密码,因此您无需遍历字符。 The big clue is that you never use item within the loop -- so you are not really iterating through the string.重要的线索是你从不在循环中使用item —— 所以你并没有真正遍历字符串。


One helpful debugging hint: make your print statements unique, so you can tell why you failed on each iteration.一个有用的调试提示:让你的打印语句独一无二,这样你就可以知道每次迭代失败的原因

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM