Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"))

password_list = []
for char in range(0 , nr_letters + 1):
    password_list.append(random.choice(letters))
#print(password_list)
    
for char in range(0 , nr_numbers + 1):
    password_list += random.choice(numbers)
    
for char in range(0 , nr_symbols + 1):
    password_list += random.choice(symbols)
    
print(password_list)
random.shuffle(password_list)
print(password_list)

password = ""
for char in password_list:
    password += char
    
print(password)


What I have tried:

What's the purpose of the append function, I removed it and the program is working without any error but what's the purpose of this function to use here?
Thank you!
Posted
Updated 27-Sep-22 4:34am
Comments
Member 8428760 10-Oct-22 16:51pm    
The append function is like push in other languages for an array. += used more for math and concatenation. So I think it is mostly convention.

Python
password_list = []
for char in range(0 , nr_letters + 1): # generates 1 item too many
    password_list.append(random.choice(letters))

That code adds each letter to the end of the password_list array. And if you remove those lines then your code will not work correctly.


Also your range loops are wrong, they should be in the form:
Python
# if nr_letters = 5
for char in range(nr_letters): // counts from 0 to 5 - 1, this will generate 5 items.
 
Share this answer
 
v3
Comments
CPallini 20-Sep-22 14:30pm    
5.
Richard MacCutchan 20-Sep-22 15:03pm    
Thanks.
You can also use the form

numbers = list('0123456789')

It's a lot less typing and less prone to typos.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900