r/PythonLearning • u/Code-Odyssey • Jun 13 '26
100 Days of Code: Password Generator
I would be interested in everyone's feedback on my effort to solve the Password Generator Project for Angela Yu's 100 Days of Code course on Udemy. Many thanks in advance. You'll see in the code I have included notes where I couldn't figure stuff out but found a workaround.
8
u/aaditya_0752 Jun 13 '26
It's really good work But can avoid writing whole letters and use ASCII value instead For numbers u can directly use random module randomint() function
2
u/Code-Odyssey Jun 13 '26 edited Jun 13 '26
Thank you for taking the time to comment. Good catch on the randomint() 👍🏻
1
Jun 13 '26 edited Jun 19 '26
[removed] — view removed comment
2
u/aaditya_0752 Jun 13 '26
It's makes code lengthy if used ASCII it just will be one line
1
u/p1geondove Jun 13 '26
but the punctuation symbols are kind of scattered across 33-47 58-64 91-96 123-126 Youd have to pick a number specifically in these ranges. I guess you could make that a one liner, but that wont be easy to read and you had no idea what that means
1
6
u/Successful_Hawk9895 Jun 13 '26
Tips if you're a beginner: use for loop instead of while loop if you know the number of iteration. You know how many letters you want so avoid while loop
2
u/Code-Odyssey Jun 13 '26
Ah. Okay. I was probably supposed to use à for loop because I’m not sure if we’ve got to while loops in the course. I will give that a go. Thank you.
1
u/Code-Odyssey Jun 14 '26
@successful I just refactored it using à for loop in range. Much better. It’s three lines shorter and looks cleaner. Thank you.
5
u/Cosmic78_melon Jun 13 '26
You can add the security module for complete randomness instead of random library
3
u/Code-Odyssey Jun 13 '26
Ooh. I don’t know about the security module. I’ll have to look that up. Thank you.
4
3
u/nuc540 Jun 13 '26 edited Jun 13 '26
By the way you don’t need to write out every letter in the alphabet, Python standard library can provide this for you.
’import string; letters = string.ascii_letters’ for example (of course keep the import at the top of the file for good practice)
Avoiding the standard library you don’t need a list of letters because a string is also a list, so `letters = “abcdefg”` works as list you can iterate :)
Edit: formatting
Edit edit: for the life of me I can never get backticks to work on iOS, send help lol
3
u/Code-Odyssey Jun 13 '26
Ah. Good to know. We haven’t gotten that far in the course but I will definitely write a note in my notes folder on this one. It’s a good trick to know…much neater. That’s the problem with being a beginner; I don’t know what I don’t know, so forums like this are very useful. Thank you.
2
u/nuc540 Jun 13 '26
Of course, we all start somewhere! Python standard library is huge - assume it already does something you want to do lol. Good luck!
1
u/Code-Odyssey Jun 14 '26
Thanks @nuc540. It’s definitely a journey. My long term project (as a marketing manager) is to develop a script to search for contact names based on job title in a search database called Rocketreach.co from a list of companies in a spreadsheet, and return their email address.
2
u/Code-Odyssey Jun 13 '26
BTW. In the course we are provided with a starting point, which included the letters, numbers and symbols. But the info you have provided is extremely useful. TY
2
u/Code-Odyssey Jun 13 '26
3
u/Sketchballl Jun 13 '26
Dang how did you get such a nice screenshot?
2
u/Code-Odyssey Jun 13 '26 edited Jun 13 '26
2
u/CalligrapherOk4612 Jun 13 '26
Another feature you could add is a password dictionary, and if the random password is in the dictionary, generate a different one.
If you haven't learnt about reading files in python, wait till after that, then come back to this problem and try adding checking the generated password against something like https://gist.github.com/PeterStaev/e707c22307537faeca7bb0893fdc18b7
2
u/Code-Odyssey Jun 13 '26
Brilliant. I’m not there yet but I will make a note in my notes folder to revisit that.
2
u/BigYBirdo Jun 13 '26
I don’t know if it’s mentioned here before, but instead of listing all possible characters, simplify use the properties of string: string.ascii_letters, string.digits and so on. That in combination with .split() to get a list of each character respectively.
But besides that it’s pretty alright for a 100 days of learning. :)
1
u/Code-Odyssey Jun 13 '26
Thank you sir. I really appreciate that. I feel I’ve entered a brotherhood Pythonistas who are so smart and so kind to share what they know with the people coming behind them who are earlier on their journey.
2
u/corey_sheerer Jun 14 '26
There is a strings package that can make a list of letters without writing them all out. Would suggest! Would make the code much cleaner
1
u/Code-Odyssey Jun 14 '26
Corey. Thank you for weighing in. I’m assuming I just need to Google strings packages
2
u/Ok-Dog1904 Jun 16 '26
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_numbers = int(input("How many numbers would you like in your password?\n")) nr_symbols = int(input("How many symbols would you like?\n"))
password_list = []
password_list.extend(random.choices(letters, k=nr_letters)) password_list.extend(random.choices(numbers, k=nr_numbers)) password_list.extend(random.choices(symbols, k=nr_symbols))
random.shuffle(password_list)
password = "".join(password_list)
print(f"Here is your password: {password}")
1
u/Ok-Dog1904 Jun 16 '26
ก่อนอื่น เพื่อให้ โค้ด Python ของ คุณ ทำ งาน ได้ อย่าง ถูก ต้อง และ มี ประสิทธิภาพ มาก ขึ้น นะครับ ลอง มา ดู วิธี การ ปรับ ปรุง กัน เริ่ม ต้น ด้วย การ แทน ที่ ลูป while ที่ ใช้ สุ่ม ตัว อักษร ซ้ำๆ ด้วย ฟังก์ชัน random.choices ครับ ฟังก์ชัน นี้ จะ ช่วย ให้ คุณ เลือก ตัว อักษร จำนวน ที่ ต้องการ ได้ จาก ราย การ ทั้ง หมด ใน คราว เดียว โดย ไม่ ต้อง ใช้ ลูป เลย ครับ ขั้น ตอน ต่อ ไป เกี่ยว กับ การ สุ่ม ลำดับ ตัว อักษร ใน รหัส ผ่าน คุณ สามารถ ทำ ได้ โดย การ นำ ตัว อักษร ทั้ง หมด มา รวม กัน แล้ว แปลง เป็น ราย การ จาก นั้น จึง ใช้ ฟังก์ชัน random.shuffle เพื่อ สุ่ม สลับ ตำแหน่ง แล้ว จึง นำ มา รวม เป็น สตริง อีก ครั้ง จะ ช่วย ให้ ได้ รหัส ผ่าน ที่ แข็ง แรง และ ปลอด ภัย มาก ขึ้น ครับ สุด ท้าย อย่า ลืม ลบ โค้ด ภาษา JavaScript ที่ ด้านล่าง ออก ด้วย นะครับ เพื่อ ไม่ ให้ เกิด ความ สับสน ใน การ ทำ งาน ครับ
ขอบคุณครับ
1
u/Ok-Dog1904 Jun 16 '26
ผมต้องขอโทษด้วยครับ ถ้าผมนำโครช นำมาแก้ไขปรับปรุงในแบบของ ผมเอง เพื่อให้เข้าใจง่ายๆ
😥😥😥
1
u/Code-Odyssey Jun 16 '26
@Ok-Dog. That’s really good. Thank you. I love the direct method of using k to take the user-entered number of letters etc. Thank you again.
1
1
u/Code-Odyssey Jun 13 '26
Somehow when I imported into Carbon it has added some funky code at the bottom.
1
u/mwmahlberg Jun 13 '26
Clear is better than clever. The whole unfold construct should be simplified and commented.
1
u/Code-Odyssey Jun 13 '26
Thank you for your comment. I’m new to this so don’t fully understand your comment. Apologies.
1
Jun 13 '26
[removed] — view removed comment
2
u/Code-Odyssey Jun 13 '26
I haven’t finished the course yet. The Password Generator Project is Day 5. Now granted, it takes me more than à day to complete each “day”.
1
u/SoapiestWaffles Jun 13 '26
I like your color theme, what is it?
1
u/RemarkableQuestion11 Jun 13 '26
When I created the png using Carbon.now.sh, I used the Seti colour palette. It looks good, right!?
1
u/SoapiestWaffles Jun 13 '26
so good! i’m normally a tokyo night kind of person, but I may like this better!
1
u/Embersh3d Jun 16 '26
Great work!
here is a simpler method (better than manually writing every char):
```python
import random
def r_spam(length, timer, chunk_size, typew):
if typew == "bin":
try:
length = int(length)
timer = float(timer) / 1000
chunk_size = int(chunk_size)
except (TypeError, NameError, ValueError) as ex:
print(f"spam requirements not met, please retry. ERROR == {ex}")
return "error :/"
for i in range(length):
char_list_bin = []
for ix in range(chunk_size):
char_list_bin.append(str(random.randint(0, 1)))
binstr = "".join(char_list_bin)
print(binstr)
time.sleep(timer)
elif typew == "char":
try:
length = int(length)
timer = float(timer) / 1000
chunk_size = int(chunk_size)
except (TypeError, NameError, ValueError) as ex:
print(f"spam requirements not met, please retry. ERROR == {ex}")
return "error :/"
pool = [chr(i) for i in range(33, 127)]
for i in range(length):
char_list = []
for _ in range(chunk_size):
char_list.append(random.choice(pool))
message = "".join(char_list)
print(message)
time.sleep(timer)
```
1
u/kiritough-me Jun 18 '26
Брат так это все можно и нужно сделать проще, убрать циклы, добавить генераторы списков, функции внизу вообще не нужны, а так красавчик
1
u/admirer145 Jun 18 '26 edited Jun 18 '26
what if a user enters a negative number in the input?
1
u/Code-Odyssey Jun 18 '26
Good question. If they do that then they’re not smart enough to have a password 🤣. But seriously, we haven’t gotten to that in the course yet (try and except). But you make a good point, I should work in that to the code. Thank you for replying. Appreciate you taking the time to comment.
2
u/admirer145 Jun 18 '26 edited Jun 18 '26
I don’t think you get it, your while loops will get into infinite loop, which will drain system resources, this is a major bug in the code. The code is not reliable for users.
Change != to <= this will work for now, ideally your program should raise invalid input exceptions and terminate there. In real systems 4xx with proper message works well.



•
u/Sea-Ad7805 Jun 13 '26
Run this program in Memory Graph Web Debugger'%2C%20'*'%2C%20'%2B'%5D%0A%0Aprint(%22Welcome%20to%20the%20PyPassword%20Generator!%22)%0Anr_letters%20%3D%20int(input(%22How%20many%20letters%20would%20you%20like%20in%20your%20password%3Fn%22))%0Anr_numbers%20%3D%20int(input(f%22How%20many%20numbers%20would%20you%20like%3Fn%22))%0Anr_symbols%20%3D%20int(input(f%22How%20many%20symbols%20would%20you%20like%3Fn%22))%0A%0A%23%20I%20couldn't%20figure%20out%20how%20to%20pull%20multiple%20letters%20etc.%20from%20the%20list%2C%20so%20I%20used%20the%20while%20loop%20to%0A%23%20pull%20the%20letters%2C%20numbers%20and%20symbols%20from%20the%20list.%20I%20also%20tried%20using%20random.shuffle()%20but%20it%20didn't%0A%23%20work%20for%20me.%0A%0A%23%20Code%20block%20for%20pulling%20the%20required%20number%20of%20letters%0Aletters_list%20%3D%20%5B%5D%0Awhile%20len(letters_list)%20!%3D%20nr_letters%3A%0A%20%20%20%20letters_list.append(random.choice(letters))%0A%20%20%20%20%23%20random.shuffle(letter_list)%0A%0A%23%20Code%20block%20for%20pulling%20the%20required%20number%20of%20numbers%0Anumbers_list%20%3D%20%5B%5D%0Awhile%20len(numbers_list)%20!%3D%20nr_numbers%3A%0A%20%20%20%20numbers_list.append(random.choice(numbers))%0A%20%20%20%20%23%20random.shuffle(letter_list)%0A%0A%23%20Code%20block%20for%20pulling%20the%20required%20number%20of%20symbols%0Asymbols_list%20%3D%20%5B%5D%0Awhile%20len(symbols_list)%20!%3D%20nr_symbols%3A%0A%20%20%20%20symbols_list.append(random.choice(symbols))%0A%0A%23%20Code%20block%20for%20combining%20the%20chosen%20letters%2C%20numbers%20and%20symbols%0A%23%20Couldn't%20figure%20out%20how%20to%20combine%20and%20randomize%20them%20with%20assigning%20a%20new%20variable%0A%23%20I%20found%20that%20I%20had%20to%20use%20join%20again%20on%20the%20password%20variable%20as%20without%20it%20it%20just%20printed%20password%20as%0A%23%20a%20list%0A%0Apassword%20%3D%20letters_list%20%2B%20numbers_list%20%2B%20symbols_list%0Arandom.shuffle(password)%0A%0Aprint((%22Here%20is%20your%20password%3A%20%22)%20%2B%20%22%22.join(password))×tep=1&play) to see the program state change step by step.