Dr. Angela의 100일 간의 코드 Python 부트캠프 중 05일

Day 05 - For 루프, 범위 및 코드 블록

이 날을 위해 For Loops 및 random.shuffle() 방법을 사용하여 암호 생성기를 만들었습니다.


import random
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']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?\n")) 
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

#Order of characters randomised:
#e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P
password = ""
random_password = ""

for i in range (0, nr_letters): #loop through letters using range of input
  letters_index = random.randint(0,len(letters)-1) #get random item on letters
  password += letters[letters_index]

for i in range (0, nr_symbols): #loop through symbols using range of input
  symbol_index = random.randint(0, len(symbols)-1) #get random item on symbols
  password += symbols[symbol_index]

for i in range (0, nr_numbers): #loop through numbers using range of input
  number_index = random.randint(0, len(numbers)-1) #get random item on numbers
  password += numbers[number_index]

splitted_pass = list(password)
random.shuffle(splitted_pass)

for word in splitted_pass:
    random_password += str(word)
print(random_password)



산출:

좋은 웹페이지 즐겨찾기