def问题 - NameError:name' passwordchecker'没有定义

时间:2017-11-12 17:35:26

标签: python

我正在制作一个密码检查器和一个带菜单的生成器,密码检查器由它自己工作正常但菜单不能与代码一起工作,我已经通过它尝试了菜单&# 39;自我,这也不起作用。这些是我收到的错误:

Traceback (most recent call last):
  File "C:/Users/Jasiu Czajka/PycharmProjects/untitled/First Code.py", line 41, in <module>
    mainmenu()

  File "C:/Users/Jasiu Czajka/PycharmProjects/untitled/First Code.py", line 24, in mainmenu
    passwordchecker()
NameError: name 'passwordchecker' is not defined

我不确定自己做错了什么,所以请尽可能帮忙。

我使用pycharm和python 3.6.3

import re


def mainmenu():
    print("*******************************************************************")
    print("           Welcome to the Password Checker & Generator             ")
    print('*******************************************************************')
    print("-------------------------------------------------------------------")
    print("This program can be used to check a password to see if it is strong")
    print("-------------------------------------------------------------------")
    print("This Program can be used to generate strong passwords")
    print("-------------------------------------------------------------------")
    print("1. Password Checker")
    print("-------------------------------------------------------------------")
    print("2. Password Generator")
    print("-------------------------------------------------------------------")
    print("3. Exit")
    print("-------------------------------------------------------------------")
    print("*******************************************************************")
    while True:
        try:
            selection = int(input("Enter choice:  "))  # Making selection a variable
            if selection == 1:
                passwordchecker()
                break
            elif selection == 2:
                passwordgenerator()
                break
            elif selection == 3:
                exit()
                break
            else:
                print("Invalid Choice. Enter 1-3")
                mainmenu()

        except ValueError:
            print("Invalid Choice. Enter 1-3")
    exit()


mainmenu()


def passwordchecker():
    print("***************************************************************")
    print("                       PASSWORD CHECKER                        ")
    print("***************************************************************")
    print("                                                               ")
    print("---------------------------------------------------------------")
    print("The password must be at least 8 characters, and a maximum of 24")
    print("---------------------------------------------------------------")
    print("The Password must contain at least 1 uppercase letter")
    print("---------------------------------------------------------------")
    print("The Password must contain at least 1 lowercase letter")
    print("---------------------------------------------------------------")
    print("The password must at least have 1 number in it")
    print("---------------------------------------------------------------")
    print('The password must have at least 1 symbol')
    print("Allowed Symbols: !, $, %, ^, &, *, (, ), _, -, +, =, ")
    print("---------------------------------------------------------------")

    incorrectpassword = True

    while incorrectpassword:
        password = input("Type in your password: ")
        if len(password) < 8:
            print("Your password must be at least 8 characters long")
        elif len(password) > 24:
            print("Your password must be maximum 24 characters long")
        elif not any(i.isdigit() for i in password):
            print("You need a number in your password")
        elif not any(i.isupper() for i in password):
            print("You need a capital letter in your password")
        elif not any(i.islower() for i in password):
            print("You need a lowercase letter in your password")
        elif re.search('[!, $, %, ^, &, *, (, ), _, -, +, =,]', password) is None:
            print("You need a symbol in your password")
        else:
            print("Your password has all the characters needed")
            incorrectpassword = False


passwordchecker()
mainmenu()


def passwordgenerator():
    print("Work In Progress")

3 个答案:

答案 0 :(得分:0)

#body

答案 1 :(得分:0)

您需要将调用移至mainmenu(),使其低于passwordchecker()passwordgenerator()的定义。否则,mainmenu()尝试调用它们时不会定义它们。

import re


def mainmenu():
    print("*******************************************************************")
    print("           Welcome to the Password Checker & Generator             ")
    print('*******************************************************************')
    print("-------------------------------------------------------------------")
    print("This program can be used to check a password to see if it is strong")
    print("-------------------------------------------------------------------")
    print("This Program can be used to generate strong passwords")
    print("-------------------------------------------------------------------")
    print("1. Password Checker")
    print("-------------------------------------------------------------------")
    print("2. Password Generator")
    print("-------------------------------------------------------------------")
    print("3. Exit")
    print("-------------------------------------------------------------------")
    print("*******************************************************************")
    while True:
        try:
            selection = int(input("Enter choice:  "))      # Making selection a variable
            if selection == 1:
                passwordchecker()
                break
            elif selection == 2:
                passwordgenerator()
                break
            elif selection == 3:
                exit()
                break
            else:
                print("Invalid Choice. Enter 1-3")
                mainmenu()

        except ValueError:
            print("Invalid Choice. Enter 1-3")
    exit()




def passwordchecker():

    print("***************************************************************")
    print("                       PASSWORD CHECKER                        ")
    print("***************************************************************")
    print("                                                               ")
    print("---------------------------------------------------------------")
    print("The password must be at least 8 characters, and a maximum of 24")
    print("---------------------------------------------------------------")
    print("The Password must contain at least 1 uppercase letter")
    print("---------------------------------------------------------------")
    print("The Password must contain at least 1 lowercase letter")
    print("---------------------------------------------------------------")
    print("The password must at least have 1 number in it")
    print("---------------------------------------------------------------")
    print('The password must have at least 1 symbol')
    print("Allowed Symbols: !, $, %, ^, &, *, (, ), _, -, +, =, ")
    print("---------------------------------------------------------------")

    incorrectpassword = True

    while incorrectpassword:
        password = input("Type in your password: ")
        if len(password) < 8:
            print("Your password must be at least 8 characters long")
        elif len(password) > 24:
            print("Your password must be maximum 24 characters long")
        elif not any(i.isdigit() for i in password):
            print("You need a number in your password")
        elif not any(i.isupper() for i in password):
            print("You need a capital letter in your password")
        elif not any(i.islower() for i in password):
            print("You need a lowercase letter in your password")
        elif re.search('[!, $, %, ^, &, *, (, ), _, -, +, =,]', password) is None:
            print("You need a symbol in your password")
        else:
            print("Your password has all the characters needed")
            incorrectpassword = False


passwordchecker()


def passwordgenerator():

    print("Work In Progress")

mainmenu()
mainmenu()

答案 2 :(得分:0)

似乎你在定义之前调用了一些函数。在mainmenu()之前放置passwordgenerator()和passwordchecker(),然后根据需要调用它们。

另外,我可以建议将这个程序放在一个循环中,并且 使该循环依赖于变量。这样会更优雅。其次,将您的功能重命名为:

mainMenu()
passwordGenerator()
passwordChecker()

看起来更专业。祝你好运!