Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While loop repeat
#1
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
Reply
#2
Could you please edit your message with python in brackets [].
It would be much easier to read because We can see where are the if or while loops.
With(without the "" and the spaces between python and the brackets):
"[ python ]"
"[ /python ]"

I didn't know the existence of the "re.search" function.
But here is something simple that works (I didn't write the whole alphabet):
ar=['a','b','c','d','e','f','g']
def password_1(x):
    i=0
    while password == ' ':
        print("Please enter a valid password")
    while i<len(password) and (password[i] in ar):
        i+=1
    if i>len(password)-1:
        print("valid")
    else:
        print("invalid")
password = input("Please enter your password: ")
password_1(password)
Reply
#3
Unable to edit due to account restrictions. Thanks for the code
Reply
#4
You're welcome.
Reply
#5
@MasterJediKnight7 here is the code for the function which is 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)

@JediMasterKnigh7 and here is the first method which has more lines of code but repeats the question for user input until the conditions are met:

#///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")
Reply
#6
I'm sorry, I can't help you right now.
I'm kind of on a tight schedule.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,358 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Repeat request by else stsxbel 2 1,185 Jul-30-2022, 03:34 PM
Last Post: stsxbel
  get out of while loop and stop repeat Frankduc 11 3,001 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  Avoid multiple repeat in indent Frankduc 8 2,908 Jan-18-2022, 05:46 PM
Last Post: Frankduc
  How to discard list repeat values akanowhere 7 3,718 Dec-28-2020, 09:14 PM
Last Post: akanowhere
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,803 Nov-23-2020, 11:01 PM
Last Post: perfringo
  is there a way: repeat key word args Skaperen 2 2,249 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Python Script to repeat Photoshop action in folders and subfolders silfer 2 4,569 Jul-25-2019, 03:12 PM
Last Post: silfer
  Trying to Repeat a Function? DanielHetherington 1 2,352 Mar-27-2019, 09:48 PM
Last Post: SheeppOSU
  Regular expressions help re.error: multiple repeat at position 23 JoseSalazar1 2 6,653 Sep-18-2018, 01:29 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020