有关点的外观的Python帮助

时间:2017-11-11 12:01:13

标签: python

import random
import time
import re
import sys
import string
import os
from random import *

def menu():
    print("Welcome To A Password Generator And Checker")
    while True:
        try:
            choice = int(input("""
1) Generate Password
2) Check Password
3) Quit
"""))

            if choice == 1:
                gp()
            elif choice == 2:
                print("""
- Requirments -

You Must Include:
~ Uppercase Letter
~ Lowercase Letter
~ Number
~ Symbol
""")
                time.sleep(1)
                print("""
Point System:
~1 Uppercase Letter = 5 Points
~1 Lowercase Letter = 5 Points
~1 From 0-9 = 5 Points
~1 Allocated Symbol = 5 Points
~If Has All Add 10 Points
    """)
                passwd = input("Enter Your Desired Password: ")
                passwdVal(passwd)
            elif choice == 3:
                os.system("cls")
                print("Goodbye")
                time.sleep(0.5)
                sys.exit()
        except ValueError:
                    os.system("cls")
                    print("Please select a legitimate option")







def passwdVal(passwd):
    points = 0
    while passwd:  
        if (len(passwd)<8 or len(passwd)>24):
            print("Your Length Is Either Too Big Or Too Small")
            print("Try Again")
            return
        if not re.search("[a-z]",passwd):
            break
        elif not re.search("[0-9]",passwd):
            break
        elif not re.search("[A-Z]",passwd):
            break
        elif not re.search("[!$%^&()_]",passwd):
            break
        else:
            print("Valid Password")
            return



    while points < 35:
        if passwdVal(passwd):
            if 8 <= len(passwd) < 24 :
                print(len(passwd),"Points Added - Length")
                points += (len(passwd))

            else:
                print("Wrong length, it must be 8 to 24 characters")
                continue

            if re.search("[a-z]", passwd):
                print("5 Points Added - Lowercase Letter")
                points += 5

            if re.search("[0-9]", passwd):
                print("5 Points Added - Number")
                points += 5

            if re.search("[A-Z]", passwd):
                print("5 Points Added - Uppercase Letter")
                points += 5

            if re.search("[!$%^&()_]", passwd):
                print("5 Points Added - Symbols")
                points += 5

            if points == 20:
                points += 10

            print("You have {} points".format(points))
            break
        else:
            print("Not a Valid Password")











def gp():
    print("""
Generating Password:""")
    generation = string.ascii_letters+("[!$%^&*()-_=+]")+string.digits
    gp = ("").join(choice(generation) for x in range(randint(8,12)))
    time.sleep(1)





menu()

我已经给出了整个代码,以防问题出现在其他代码中。然而,我面临的问题是,当我输入有效密码时,它表示有效密码,好的。但这些要点并没有显示出来。我已经尝试了很多次,但我找不到问题。当我输入错误的密码时,它显示我想要的正确的东西。

2 个答案:

答案 0 :(得分:0)

在您打印passwdVal(passwd)后,您的函数return有一个print("Valid Password")语句,这意味着后续代码(总结点数的代码)不会被执行。

答案 1 :(得分:0)

让我们仔细看看您的密码验证功能。 menu()gp()没有最好的代码风格,但至少它们似乎有效。

您的passwdVal(),一步一步评论

points = 0
while passwd:  # Loop while there is a password...
    if (len(passwd)<8 or len(passwd)>24):
        print("Your Length Is Either Too Big Or Too Small")
        print("Try Again")
        return  # Exit function
    if not re.search("[a-z]",passwd):
        break  # Exit loop
    elif not re.search("[0-9]",passwd):
        break  # Exit loop
    elif not re.search("[A-Z]",passwd):
        break  # Exit loop
    elif not re.search("[!$%^&()_]",passwd):
        break  # Exit loop
    else:
        print("Valid Password")
        return  # Exit function

因此,只要您的while是非空字符串,您就会创建一个True - 循环,其条件的计算结果为passwd。由于您未对此变量进行任何更改,因此如果您不强制断开循环或退出函数,则循环条件将始终保持为true。

如果密码的长度不符合您的范围,则退出整个功能。如果某些组中缺少字符,则只需退出循环并继续使用该函数的后续代码,如果密码有效,则退出该函数。

这意味着,如果您希望在密码字符串中找不到某些字符,那么解释器的计算点的部分甚至只能 ,因为在所有其他情况下,您退出整个功能。这似乎在逻辑上是不正确的,虽然它是有效的代码。您很可能希望删除while passwd:循环,因为此处不需要。在任何if - 分支上(即发现任何错误),您只想打印警告并继续使用该函数,即使在通用else - 分支上也是如此。确保不要过早退出该功能。

打开后续代码:

while points < 35:
    if passwdVal(passwd):  # This is a RECURSIVE CALL!
        if 8 <= len(passwd) < 24 :
            print(len(passwd),"Points Added - Length")
            points += (len(passwd))
        else:
            print("Wrong length, it must be 8 to 24 characters")
            continue
        if re.search("[a-z]", passwd):
            print("5 Points Added - Lowercase Letter")
            points += 5
        if re.search("[0-9]", passwd):
            print("5 Points Added - Number")
            points += 5
        if re.search("[A-Z]", passwd):
            print("5 Points Added - Uppercase Letter")
            points += 5
        if re.search("[!$%^&()_]", passwd):
            print("5 Points Added - Symbols")
            points += 5
        if points == 20:
            points += 10

        print("You have {} points".format(points))
        break
    else:
        print("Not a Valid Password")

您打算为在密码中找到的每个字符类添加点数。你实际做的是(现在忽略上面的递归调用)你为所有方面添加点,只要到目前为止给出的点数少于35。这意味着,您可能只评估几个方面而不是一次。你也不想要一个循环;只需浏览您的方面并根据需要添加点数。最后,打印你的积分。

必须删除那里的递归调用,因为它会导致无限的函数调用堆栈。

好像你应该考虑将验证功能分成一个功能来实现密码的实际验证,另一个功能是计算密码的分数。