I've created a while True loop at the end of the program, and a function repeat() that checks if it's an empty string which should just restart the program if ran in the loop. Right? I'm not sure if it's version specific or not...
import string
import random
ascii = string.ascii_letters
digits = string.digits
punct = string.punctuation
characters = ascii digits punct
def repeat():
check = ""
if check.upper() == "":
print(password)
myList = list(characters)
random.shuffle(myList)
passwordraw = myList[:15]
password = ''.join(map(str, passwordraw))
while True:
repeat()
break
CodePudding user response:
You are breaking out of a while loop after calling repeat() only once.
Remove the break to continuously call the repeat() method in the loop.
Simply just:
while True:
repeat()
Side note: I would add some kind of delay after calling repeat(), if that's what you're into. Something like:
# don't forget to import time
import time
# the rest of your code
while True:
repeat()
time.sleep(3) # Sleep for 3 seconds
Now your program will wait 3 seconds after running repeat() again.
CodePudding user response:
The first step is to clean up the code. The creation of the password boils down to:
import random
import string
def main():
characters = list(string.ascii_letters string.digits string.punctuation)
random.shuffle(characters)
password = ''.join(characters[:15])
print(password)
if __name__ == '__main__':
main()
Your description isn't quite clear in what you want to check. For the moment I assume you want the user to enter the password and to repeat the whole process of password generation and the check until the generated password matches the input of the user.
def main():
characters = list(string.ascii_letters string.digits string.punctuation)
while True:
random.shuffle(characters)
password = ''.join(characters[:15])
print(password)
password_input = input('Enter the password: ')
if password == password_input:
break
Now you can only leave the loop if you enter the correct random password (with 15 different characters). It's highly unlikely that you are able to do that (that's why I left the print in the code) and I'm not sure that this is what you want the program to do. You might want to clarify your question and explain what the "it" in "if it's an empty string" is.
CodePudding user response:
A while loop will loop through any code written inside the loop only whilst the parameters are met. in your case whilst true it will be repeated however when false it will not. if you want to change what is looped, change what is inside your loop parameters.
CodePudding user response:
no this in not version specific
