In: Computer Science
I have to write a random password generator in Python 3 and im having some trouble writing the code and i dont really know where to start. The prompt goes as such:
The problem in this assignment is to write a Python program that uses functions to build (several options of) passwords for the user. More specifically, your program will do the following:
1. Prompt the user for the length of the password to be generated. Call this length lenP.
2. The password can be made of: (a) lower case letters (nLower) (b) upper case letters (nUpper) (c) digits (between 0 and 9) (nDigits) (d) one special character from this collection: !, @, #, $, %, *, = So the program must also prompt the user for how many lower case letters, how many upper case letters, and how many digits, they want in the password. These values will be stored in nLower, nUpper, and nDigits, respectively. There will be only and exactly one special character on every password.
3. First make sure that lenP = nLower + nUpper + nDigits + 1. In other words that all those characters will fit in a password of length lenP.
4. Using the numbers given by the user, the program must pick randomly: nLower lower case letters from the collection of those letters, nUpper from the collection of all the upper case letters, and nDigits from the ten digits.
5. Pick randomly one special character from the seven characters listed above.
6. Build a password string with all the lower, upper case letters, digits, and the special character, picked randomly in the steps 4 and 5 above, by placing each of those “letters”, numbers, and special character in a random place on the password.
7. After builiding ONE password in the above fashion, shuffle all the characters in the password to produce three other passwords, all with the same characters but in different orders.
8. The final output to the user is all four passwords produced in all the steps above. The program should only print the prompts and interaction with the user and these four passwords with a message to the user showing them, nothing else should be printed by this program.
I must also include:
• A function called pickSome(n, s) Given the number n, this function will randomly select n characters from the string s and will return them in another string. For example, to pick 2 upper case letters from all the upper case letters, you should call this function as: pickSome(2,’ABCDEFGHIJKLMNOPQRSTUVWXYZ’), the function will return a string with the two upper case letters selected randomly. For example, that call could return LC as a string.
• A function called putTogether(forPasswd) the function takes a string of letters (upper and lower case), digits, and one special character and will place each of the characters in the string in a random location on a new string. It returns the new string built by placing the characters in the string passed as argument in random order. For example, when this function is called as: putTogether(’aceLP29=’) it could return the following new string: 2aLce9P=
• A function called shuffle(passwd) which given the string passwd, it will randomly reorder the characters in that string and return the new string
there is so much to this and i dont really know where to start
import random
def pickSome(n,s):
#convert string to list
l = list(s)
# In-place shuffle
random.shuffle(l)
# Take the first 2 elements of the now randomized array
m=l[0:n]
#convert it to string again
result = ''.join(m)
return result
def putTogether(s):
l=list(s)
# In-place shuffle
random.shuffle(l)
result = ''.join(l)
return result
def shuffle(s):
l=list(s)
pr=[]
for i in range (0,3):
random.shuffle(l)
result = ''.join(l)
print(result)
print("Enter length of the password")
lenP=int(input())
print("Enter number of lower case letters in the password")
nLower=int(input())
print("Enter number of upper case letters in the password")
nUpper=int(input())
print("Enter number of digits in the password")
nDigit=int(input())
if(lenP==nLower+nUpper+nDigit+1):
pas=''
pas+=pickSome(nUpper, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
pas+=pickSome(nLower, 'abcdefghijklmnopqrstuvwxyz')
pas+=pickSome(nDigit, '01234567890')
pas+=pickSome(1, '!@#$%*=')
print("Your passwords:")
print(putTogether(pas))
shuffle(pas)