Advertisement
RadoG

Angela's Python Course -- Create a Password Generator -- no shuffle

Jan 27th, 2022
939
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Password Generator Project
  2. import random
  3. letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  4. numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  5. symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
  6.  
  7. print("Welcome to the PyPassword Generator!")
  8. nr_letters= int(input("How many letters would you like in your password?\n"))
  9. nr_symbols = int(input(f"How many symbols would you like?\n"))
  10. nr_numbers = int(input(f"How many numbers would you like?\n"))
  11.  
  12. #Eazy Level - Order not randomised:
  13. #e.g. 4 letter, 2 symbol, 2 number = JduE&!91
  14.  
  15. # # initiating an empty variable; it can't be equal to 0, since we are not going to deal with math ops here
  16. # password = ""
  17.  
  18. # # adding letters
  19. # for letter in range(1,  nr_letters + 1):
  20. #     # print(letter)
  21. #     password += letters[random.randint(0, len(letters) - 1)]
  22.  
  23. # # adding numbers
  24. # for number in range(1, nr_numbers + 1):
  25. #     password += numbers[random.randint(0, len(numbers) -1)]
  26.  
  27. # # adding symbols
  28. # for symb in range(1, nr_symbols + 1):
  29. #     password += symbols[random.randint(0, len(symbols) - 1)]
  30.  
  31. # print(password)
  32.  
  33.  
  34. #Hard Level - Order of characters randomised:
  35. #e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P
  36.  
  37. password = ""
  38.  
  39. # counter initiation
  40. count_letters = nr_letters
  41. count_numbers = nr_numbers
  42. count_symbols = nr_symbols
  43.  
  44. # password lenght calc
  45. password_len = nr_letters + nr_numbers + nr_symbols
  46.  
  47. # type randomness initiation
  48. type_rand = 0
  49.  
  50.     # the point if the for loop is to have a limited runs and each run should count
  51.     # the problem is that we have a limit for the letters, another for numbers and the 3rd one for the symbols
  52.     # if we already added all symbols, let's say 5 out of 5 and the random type selects 0 for letters, we will have a run of the loop where not char is added to the password, which ultimately will make our password shorter than desired
  53.     # the SOLUTION I can think of is to the check where are we with the counters at each run of the for-loop:
  54.     #  - we have still have letters, symbols and numbers to spend, OK - we will random among all 3 char types
  55.     #  - if not, we will select among the type we have, so there will be a check, just a if
  56.  
  57.  
  58. for run in range(1, password_len + 1):
  59.     # # refreshing the type value with a new ramdom int btn 0 and 2
  60.     # type_rand = random.randint(0, 2)
  61.  
  62.     # # adding to chars to the password depending on the random value
  63.  
  64.     # # adding letters
  65.     # if type_rand == 0 and count_letters > 0:
  66.     #     count_letters -= 1
  67.     #     password += letters[random.randint(0, len(letters) - 1)]
  68.    
  69.     # # adding numbers
  70.  
  71. # case 1 - we have all types
  72.     if count_letters > 0 and count_numbers > 0 and count_symbols > 0:
  73.         # random for 3 values: 0 - letters, 1 - numbers, 2 - symbols
  74.         type_rand = random.randint(0, 2)
  75.  
  76.         # adding char of type as per the random value if all 3 types are still available
  77.         if type_rand == 0:
  78.             count_letters -= 1
  79.             password += letters[random.randint(0, len(letters) - 1)]
  80.         elif type_rand == 1:
  81.             count_numbers -= 1
  82.             password += numbers[random.randint(0, len(numbers) - 1)]
  83.         elif type_rand == 2:
  84.             count_symbols -= 1
  85.             password += symbols[random.randint(0, len(symbols) - 1)]
  86.  
  87.  
  88. # case 2 - NO Symbols to use
  89.     if count_letters > 0 and count_numbers > 0 and count_symbols == 0:
  90.        
  91.         # random for 2 values: 0 - letters, 1 - numbers
  92.         type_rand = random.randint(0, 1)
  93.        
  94.         # adding char of type as per the random value if only letters and numbers are available
  95.         if type_rand == 0:
  96.             count_letters -= 1
  97.             password += letters[random.randint(0, len(letters) - 1)]
  98.         elif type_rand == 1:
  99.             count_numbers -= 1
  100.             password += numbers[random.randint(0, len(numbers) - 1)]
  101.  
  102. # case 3 - NO Numbers to use
  103.     if count_letters > 0 and count_numbers == 0 and count_symbols > 0:
  104.        
  105.         # random for 2 values: 0 - letters, 1 - symbols
  106.         type_rand = random.randint(0, 1)
  107.        
  108.         # adding char of type as per the random value if only letters and symbols are available
  109.         if type_rand == 0:
  110.             count_letters -= 1
  111.             password += letters[random.randint(0, len(letters) - 1)]
  112.         elif type_rand == 1:
  113.             count_symbols -= 1
  114.             password += symbols[random.randint(0, len(symbols) - 1)]
  115.  
  116.            
  117. # case 4 - NO Letters to use
  118.     if count_letters == 0 and count_numbers > 0 and count_symbols > 0:
  119.        
  120.         # random for 2 values: 0 - numbers, 1 - symbols
  121.         type_rand = random.randint(0, 1)
  122.        
  123.         # adding char of type as per the random value if only numbers and symbols are available
  124.         if type_rand == 0:
  125.             count_numbers -= 1
  126.             password += numbers[random.randint(0, len(numbers) - 1)]
  127.         elif type_rand == 1:
  128.             count_symbols -= 1
  129.             password += symbols[random.randint(0, len(symbols) - 1)]        
  130.  
  131.  
  132. # case 5 - Only Letters
  133.     if count_letters > 0 and count_numbers == 0 and count_symbols == 0:
  134.        
  135.         # no need to random the type - only letters        
  136.         count_letters -= 1
  137.         password += letters[random.randint(0, len(letters) - 1)]
  138.        
  139.  
  140. # case 6 - Only Numbers
  141.     if count_letters == 0 and count_numbers > 0 and count_symbols == 0:
  142.        
  143.         # no need to random the type - only numbers        
  144.         count_numbers -= 1
  145.         password += numbers[random.randint(0, len(numbers) - 1)]
  146.  
  147.  
  148. # case 7 - Only Symbols
  149.     if count_letters == 0 and count_numbers == 0 and count_symbols > 0:
  150.        
  151.         # no need to random the type - only numbers        
  152.         count_symbols -= 1
  153.         password += symbols[random.randint(0, len(symbols) - 1)]
  154.  
  155.  
  156. # case 8 - Nothing left, total number of runs achieved
  157. # this case should never be reached
  158.  
  159.  
  160. print(password)
  161.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement