I'm trying to write a Python program that prompts the user for a password. It must meet the following requirements:
- no less than 6 characters in length
- no more than 12 characters in length
- at least 1 numerical digit
- at least 1 alphabetical character
- no spaces
I can get through requirements 1-3, but as soon as I put in requirement 4, it stops working. I haven't even gotten to requirement 5 because I'm currently stuck. Any help is greatly appreciated! TIA!
Here is my code:
# --- Main ----------
def main():
#display student info
studentInfo()
#display welcome message
welcomeMsg()
#prompt user for a password
passWord = input("\nPlease create a password:\n")
#call function for password length validation
passWord = correctPW(passWord)
# --- Functions ----------
#student info
def studentInfo():
print("\nName:\tNAME")
print("Class:\tCMIS102")
print("Date:\t26 July 2022")
#welcome message
def welcomeMsg():
print("\nThis program will prompt the user to enter a password with the following requirements:")
print("\t- No less than 6 characters in length")
print("\t- No more than 12 characters in length")
print("\t- No spaces")
print("\t- At least one numerical digit")
print("\t- At least one alphabetical character")
#validate password requirements
def correctPW(passWord):
#check for minimum character requirement
while (len(passWord) < 6) or (len(passWord) > 12):
print("\nSorry! Your password is invalid.")
print("It must be no less than 6 characters and no more than 12 characters in length.")
passWord = input("\nPlease create a password:\n")
#check for one numerical digit and alphabetical character requirement
while (passWord.isdigit() < 1):
print("\nSorry! Your password is invalid.")
print("It must contain at least one numerical digit.")
passWord = input("\nPlease create a password:\n")
while (passWord.isalpha() < 1):
print("\nSorry! Your password is invalid.")
print("It must contain at least one alphabetical character.")
passWord = input("\nPlease create a password:\n")
#display if all password requirements are met
if (len(passWord) >= 6) and (len(passWord) <= 12) and (passWord.isdigit() >= 1) and (passWord.isalpha() >= 1):
print("\nCongratulations! Your password is valid!")
# --- Execute ----------
main()
passWord.isdigit() < 1,passWord.isalpha() < 1, and the rest are doing?any,any(x.isalpha() for x in pswd)is probably what you want.isdigitwill check that all characters are digits and returnTrue/False.isalphawill always returnFalseifisdigitreturnsTrue.