I am making a password checker and a generator with a menu, the password checker by it's self works fine but the menu does not work along with the code and I've tried the menu by it's self and that does not work either. These are the errors that I am receiving:
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
I'm not sure what I've done wrong, so please help if you can.
I use pycharm and 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")
import re
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
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()
passwordchecker()
mainmenu()
def passwordgenerator():
print("Work In Progress")
It seems you have called some functions before they were defined. Place passwordgenerator() and passwordchecker() before mainmenu() and then call them as you please.
Also, may I suggest putting this program in a loop, and
make that loop dependent on a variable. It would be much more elegant that way. Second, rename your functions like:
mainMenu()
passwordGenerator()
passwordChecker()
It looks more professional. Good luck!
You need to move your call to mainmenu() to below the definitions for passwordchecker() and passwordgenerator(). Otherwise, they won't be defined when mainmenu() tries to call them.
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()
Related
Here, I am trying to validate a password and check to see if there are capitals and numbers in the password before saving it as a different variable. Using functions, is it possible for me to do so. The indents have changed on the format, so please help me with other aspects.
def length(long):
while len(long) < 10:
print("Please make your password longer, up to at least 10 characters.")
print("Your password is only " + str(len(long) + " characters long")
if password.isupper() = True:
print("Welcome to this student interface")
username = input("Please enter a username")
password = input("Please enter a strong password")
length(password)
This is what I have now done:
def length(long):
bool LengthCheck = False
if len(long) < 10:
print("Please make your password longer, up to at least 10 characters.")
print("Your password is only " + str(len(long) + " characters long")
else:
LengthCheck = True
errors = []
print("Welcome to this student interface")
username = input("Please enter a username")
password = input("Please enter a strong password")
length(password)
bool Capcheck = False
bool DigCheck = False
while CapCheck = False or CapCheck = False:
length(password)
if not any(x.isupper() for x in password):
errors.append("Your password needs at least 1 capital.")
else:
CapCheck = True
break
if not any(x.islower() for x in password):
errors.append("......... Why?")
if not any(x.isdigit() for x in password):
errors.append("You need to have at least 1 digit")
else:
DigCheck = True
break
if errors:
print(" ".join(errors))
password = input("Please enter a stronger password")
Apparently there is an error with my boolean here, please help
def length(long):
bool LengthCheck = False
if len(long) < 10:
print("Please make your password longer, up to at least 10 characters.")
print("Your password is only " + str(len(long) + " characters long")
else:
LengthCheck = True
Try using islower():
password.islower()
This returns True if there are no Uppercases in the password.
Now if you want to check if it has number i it, you have to follow #jubnvz:
any(i.isdigit() for i in password)
or a more specific way:
any(map(str.isdigit, password))
And for your password entries, try:
while True:
password = input(""Please enter a strong password:")
if not any(x.isupper() for x in password):
print("Your password needs at least 1 upper case.")
elif not any(x.isdigit() for x in password):
print("You need to have at least 1 digit")
elif not any(x.islower() for x in password):
print("Your password needs at least 1 lower case.")
elif len(password) < 10:
print("Please make your password longer, up to at least 10 characters.")
print("Your password is only " + str(len(password)) + " characters long")
else:
break
If you want to and a confirm password too, try:
while True:
password = input(""Please enter a strong password:")
if not any(x.isupper() for x in password):
print("Your password needs at least 1 upper case.")
elif not any(x.isdigit() for x in password):
print("You need to have at least 1 digit")
elif not any(x.islower() for x in password):
print("Your password needs at least 1 lower case.")
elif len(password) < 10:
print("Please make your password longer, up to at least 10 characters.")
print("Your password is only " + str(len(password)) + " characters long")
else:
passwordcon = input(""Please confirm your password:")
if passwordcon == password:
break
else:
print("Your passwords do not match, try again'")
any([p.isupper() for p in password])
I am trying to create a login and register program in simple console python, however when trying to make a loop that will check if the username contains a digit I keep getting the error, ("UnboundLocalError: local variable 'includesDigit' referenced before assignment") the code is:
def register():
incluesDigit = False
print("")
print("Create Account")
print("~~~~~~~~~~~~~~")
print("Username: ")
registerUsername = input("")
for char in registerUsername:
if char.isdigit():
includesDigit = True
if includesDigit == True:
print("Please enter a username that does not contain a number")
register()
print("Password: ")
registerPassword = input("")
if len(registerPassword) < 5:
print("Please enter a password that is atleast 5 characters")
register()
if len(registerPassword) > 15:
print("Please enter a password that is less than or fifteen character")
logCreate = open("C:\\Desktop\\Login Program\\Accounts\\" + registerUsername + ".txt", "w")
logCreate.write(registerPassword)
logCreate.close()
login()
There is a typo in line 2.
incluesDigit = False
Should be
includesDigit = False
I'm checking my password for this criteria
be at least 10 characters long
contain at least 1 capital letter
contain at least 1 number
contain at least one of the characters $, #, %, &, or *
not contain any spaces
my code:
password = input("enter a password ")
def passwordIsOk (password):
symbols = "$#%&*"
if len (password) > 10:
if any(i.isupper() for i in password):
if any(i.isdigit() for i in password):
if " " not in password:
for i in range(0,5):
if symbols[i] in password:
passwordValid = True
if passwordValid == True:
print("ok buddy")
else:
print("Password must contain $#%&*")
else:
print("Password must not contain spaces")
else:
print("Password must have at least 1 number")
else:
print("Password must have at least 1 capital letter")
else:
print("Password must be greater than 10 characters")
passwordIsOk(password)
It works, but It just don't feel right :(
You can avoid this kind of nested if structure by inverting the conditions. This makes the code much more readable, and puts the error messages next to the conditions which check for those errors.
def passwordIsOk(password):
symbols = "$#%&*"
if len (password) <= 10:
print("Password must be greater than 10 characters")
elif not any(i.isupper() for i in password):
print("Password must have at least 1 capital letter")
elif not any(i.isdigit() for i in password):
print("Password must have at least 1 number")
elif " " in password:
print("Password must not contain spaces")
elif not any(s in password for s in symbols):
print("Password must contain at least one of " + symbols)
else:
print("ok buddy")
password = input("enter a password ")
passwordIsOk(password)
Putting aside the nested if structure, you could use any to check if it contains any special symbol:
password = input("enter a password ")
def passwordIsOk (password):
symbols = "$#%&*"
if len (password) > 10:
if any(i.isupper() for i in password):
if any(i.isdigit() for i in password):
if " " not in password:
if any(s in password for s in symbols):
passwordValid = True
print("ok buddy")
else:
print("Password must contain $#%&*")
else:
print("Password must not contain spaces")
else:
print("Password must have at least 1 number")
else:
print("Password must have at least 1 capital letter")
else:
print("Password must be greater than 10 characters")
passwordIsOk(password)
It is arguably easier to read and it will print all the problems with the password if you invert and flatten out the if statements using the passwordValid flag like so:
password = input("enter a password ")
def passwordIsOk (password):
passwordValid = True
symbols = "$#%&*"
if len (password) <= 10:
passwordValid = False
print("Password must be greater than 10 characters")
if not any(i.isupper() for i in password):
passwordValid = False
print("Password must have at least 1 capital letter")
if not any(i.isdigit() for i in password):
passwordValid = False
print("Password must have at least 1 number")
if " " in password:
passwordValid = False
print("Password must not contain spaces")
if not any(s in password for s in symbols):
passwordValid = False
print("Password must contain at least one of $#%&*")
if passwordValid:
print("ok buddy")
passwordIsOk(password)
A common method in password validation are regular expressions.
# coding: utf-8
import re
from getpass import getpass
def passwordIsOk(password):
if len(password) < 10:
print("Password must be greater than 10 characters")
elif not re.search('[A-Z]',password):
print("Password must have at least 1 capital letter")
elif not re.search('[0-9]',password):
print("Password must have at least 1 number")
elif not re.search('[$#%&*]',password ):
print("Password must contain at least one of $#%&*")
elif " " in password:
print("Password must not contain spaces")
else:
print("ok buddy")
pswd = getpass("Enter password: ")
passwordIsOk(pswd)
I've been trying to figure this out my own, but I couldn't come up with solutions for this. I did come across to SpecialSym["$", "#", "#"] but I wasn't able to work that one into this code.
print("Password is incorrect, please try again...")
passW()
You can do this by adding the condition which would check whether any of the characters is in the list ["$", "#", "#"] or not.
The updated code would be:
import re #regular expression
print("Please enter a password to log in...")
def passW():
while True:
password=input("Enter a password:\n")
if password=="Y0urC0llege":
print("Logging in...")
print("Your login was successful.")
print("Welcome, Professor.")
break
elif len(password) < 10:
print("Please make sure your password is as least 10 characters long.")
elif re.search("[0-9]", password) is None:
print("Please contain as least 1 number in your password.")
elif re.search("[A-Z]", password) is None:
print("Please contain 1 capital letter in your password")
elif re.search("[$##]", password) is None:
print("Please contain as least 1 character symbol in your password.")
else:
print("Password is incorrect, please try again...")
passW()
Hope it helps.
You have to take special characters in a variable after that you can check the condition like below:
SpecialSym = ['!','#','#'] # You can add as many symbols you want.
elif not any(char in SpecialSym for char in password):
print("Please contain as least 1 character symbol in your password.")
incorrectPassword= True
while incorrectPassword:
password = input("type in your password: ")
if len(password) < 8:
print("your password must be 8 characters long")
if len(password) >24:
print("Your password must be shorter than 24 characters")
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")
incorrectPassword = False
How can I only allow certain characters (like !, $, %, ^, &, *, (, ), -, _, = or +) as needed input?
I would use regex, but to do it in a similar fashion to your other tests:
any(i not in '!$%^&*()-_=+' for i in password)
you can simply use
elif " " in password:
#print error
or use Python's re package to define what characters are allowed in your password