# Re-write the random password generator function below and extend it so that th
ID: 3594240 • Letter: #
Question
# Re-write the random password generator function below and extend it so that the
# passwords are written to a file. You can first ask the user what the length of the
# passwords should be, how many passwords they need to generate and
# write these to a file chosen by them.
# Optionally: Can you encrypt the passwords written to the file
# using a "master password" so that someone else can't read the
# passwords stored in the file?
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
import random
def makeRandomPassword():
password=""
numChars=requestInteger("Enter length of password")
i=0
while(i<numChars):
choice=random.randint(0,2)
if (choice==0):
password=password+ chr( random.randint(65,90) )
elif (choice==1):
password=password+ chr( random.randint(ord("a").ord("z")) )
else:
password=password+ str( random.randint(0,9) )
i=i+1
print password
Explanation / Answer
import random
def makeRandomPassword(numChars):
password=""
i=0
while(i<numChars):
choice=random.randint(0,2)
if (choice==0):
password=password+ chr( random.randint(65,90) )
elif (choice==1):
password=password+ chr( random.randint(ord("a"),ord("z")) )
else:
password=password+ str( random.randint(0,9) )
i=i+1
return password
File=open("Passwords.txt","a+")
numberOfPass=int(raw_input("Enter the number of passwords : "))
leng=int(raw_input("Enter the length of the passwords : "))
i=0
while i<numberOfPass:
File.write(makeRandomPassword(leng)+' ')
i=i+1
File.close()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.