I am new to Python (2 wks.) and outside of class have been working on a random password generator. I have made the password generator; however, I'm not really sure how to begin the process of generating another password for the user. So far, I have tried creating a loop but got lost quickly. My best guess as to what I'm doing wrong is that I just don't understand loops very well and that creating a loop will solve my problem.
# This greets the user
print('Hello, Welcome to this random password generator')
# This asks the user for the length of the password
length = int(input('\nEnter the length of the password you would like to generate: '))
# This defines data for the string module
lower = string.ascii_lowercase
upper = string.ascii_uppercase
number = string.digits
# This combines the data
all = lower upper number
# This allows us to randomize the data collected
temp = random.sample(all, length)
# This generates the password
password = "".join(temp)
# This will generate a password of up to 94 characters
print(password)
# This prompts the user to generate another password
next_generation = input("Would you like to generate another password? (yes/no): ")
CodePudding user response:
import random
import string
while True:
length = int(input("Enter password length: "))
lower = string.ascii_lowercase
upper = string.ascii_uppercase
number = string.digits
all = lower upper number
temp = random.sample(all,length)
password = ''.join(temp)
print(password)
# **** This block asks user whether to create another password ****
# If Yes, code loops back to the beginning and does it again
# If No, code stops.
another = input("Generate another password? Y/N: ").capitalize().strip()
if another == 'Y':
another = True
continue
else:
break
CodePudding user response:
You indeed need to use a loop and store data in an array or print it in the loop. This article might be of help as it explains: How to use loops to keep program running
I assume that you want your program to keep asking for new password each time and the number of passwords is not fixed. Otherwise you would use for loop.
