Reputation: 11
I'm creating a login system which includes a password strength test. A password must contain letters and numbers, uppercase and lowercase letters and be between 6 and 12 characters in length. I am being told that there is a syntax error but not where. I have only just added in the password strength test to a much larger program, and I know that the rest of the program is working perfectly and that therefore this section is the issue.
def Password():
global UName
global FName
global SName
global Email
print('A secure password should be between 6 and 12 letters in')
print('length, have upper and lowercase letters and have numbers.')
PWord = input('Please enter a secure password:')
if PWord.isalpha() or if PWord.isdigit():
if len(PWord) < 6 or if len(Pword) > 12:
if PWord.isupper() or if PWord.islower():
print('You need upper and lowercase letters. Try again')
Password()
else:
print('Password is strong')
g = open('Database1.txt', 'a')
g.write('\n' + UName +',')#Writes user details to database file
g.write(FName +',')
g.write(SName +',')
g.write(Email +',')
g.write(PWord +',')
g.close()#Closes file
h = open('Database2.txt', 'a')
Daily = input('Enter your daily energy use target:')
Monthly = input('Enter your monthly energy use target:')
Yearly = input('Enter your yearly energy use target:')
h.write('\n' + UName +',')#Writes user details to the second database file
h.write(Daily +',')
h.write(Monthly +',')
h.write(Yearly +',')
h.write('0' +',')#Other values
h.write('0' +',')#Other values
h.write('0' +',')#Other values
h.write('0' +',')#Other values
h.close()#Closes file
main()#takes user back to menu so they can login
else:
print('Password is not the correct length, try again')
Password()
else:
print('You need letters and numbers, try again')
Password()
UName, FName, SName and Email are all taken from a previous function, hence why they have been declared as global variables.
By commenting out various sections, I have determined that the line
if PWord.isalpha() or if PWord.isdigit():
is throwing up the syntax error, but I can't work out why. I have created systems like this before and can't determine the issue. If someone could help me I would be very grateful.
Upvotes: 0
Views: 157
Reputation: 224
try
if PWord.isalpha() or PWord.isdigit():
But you better have to use regex for that, as explained in others answers
Good luck
Upvotes: 1
Reputation: 1413
This is why the syntax error is here
you can put multiple conditions in the if conditions by using and
or
operators no need to use if
multiple times.
You can also use regex to validate the password here -
import re
password = raw_input("Enter string to test: ")
if re.match(r'[A-Za-z0-9@#$%^&+=]{6,12}', password):
# match
else:
# no match
Here's the explanation for the regex
[A-Za-z0-9@#$%^&+=]{6,12} " gm Match a single character present in the list below [A-Za-z0-9@#$%^&+=]{8,} {8,} Quantifier — Matches between 8 and unlimited times, as many times as possible, giving back as needed (greedy) A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive) a-z a single character in the range between a (index 97) and z (index 122) (case sensitive) 0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive) @#$%^&+= matches a single character in the list @#$%^&+= (case sensitive) Global pattern flags g modifier: global. All matches (don't return after first match) m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Upvotes: 1