Hi,
I am new to python and just playing around with basic tasks at the moment.
The project I am working on at the moment is a password validation script.
I have written this in two formats, on with just a while loop and the second
in a function with the same while loop.
When running the first method, the loop runs until the condition is met. While running the second
method, the loop runs through once and then stops. Is there a way to use the function and loop
until the condition is met or would it be with just sticking to the first method? Please see code below:
Method 1:
#///The following code works///
#import re
#password = input("Please enter a password: ")
#x = True
#while x:
# if len(password) < 8 or len(password) > 16:
# break
# elif not re.search("[a-z]",password):
# break
# elif not re.search("[A-Z]",password):
# break
# elif not re.search("[0-9]",password):
# break
# elif not re.search("[@£$]",password):
# break
# elif re.search("\s",password):
# break
# else:
# print("Valid password")
# x = False
# break
#if x:
# print("Not a valid password")
Method 2:
import re
def password_1(x):
while True:
#input("Please enter your password: ")
if re.search(r'[" ",_]', x):
print("Please enter a valid password")
elif re.match(r'[a-z,A-Z,0-9]',x) and re.search(r'[!@#$%^&*-+=]',x) and len(x) >= 8:
print("Password accepted")
break
else:
print("Please enter a valid password")
return x
password = input("Please enter your password: ")
password_1(password)
Thanks in advance
I am new to python and just playing around with basic tasks at the moment.
The project I am working on at the moment is a password validation script.
I have written this in two formats, on with just a while loop and the second
in a function with the same while loop.
When running the first method, the loop runs until the condition is met. While running the second
method, the loop runs through once and then stops. Is there a way to use the function and loop
until the condition is met or would it be with just sticking to the first method? Please see code below:
Method 1:
#///The following code works///
#import re
#password = input("Please enter a password: ")
#x = True
#while x:
# if len(password) < 8 or len(password) > 16:
# break
# elif not re.search("[a-z]",password):
# break
# elif not re.search("[A-Z]",password):
# break
# elif not re.search("[0-9]",password):
# break
# elif not re.search("[@£$]",password):
# break
# elif re.search("\s",password):
# break
# else:
# print("Valid password")
# x = False
# break
#if x:
# print("Not a valid password")
Method 2:
import re
def password_1(x):
while True:
#input("Please enter your password: ")
if re.search(r'[" ",_]', x):
print("Please enter a valid password")
elif re.match(r'[a-z,A-Z,0-9]',x) and re.search(r'[!@#$%^&*-+=]',x) and len(x) >= 8:
print("Password accepted")
break
else:
print("Please enter a valid password")
return x
password = input("Please enter your password: ")
password_1(password)
Thanks in advance