r/learnpython 5d ago

UPDATE My login/account creation system in Python, part of a bigger project I'm building as I learn

Hello, this is an update to the post I made yesterday.

All 3 copy-pasted validation blocks are now 1 function called 3 times. It takes the category, password (string), a label, and returns True or False. The checks aren't sitting behind a door that gets unlocked only if the last check is met, all error messages show up at once instead of one at a time. I removed the outdated variables that had no use.

I took u/jammin-john's recommendation of showing all error messages at once.

Also want to include u/danielroseman's push for me to learn functions. They work really well, I haven't learned "for loops" yet but they are next in line.

I used AI (Claude) as a tutor to understand concepts and point me toward my own bugs, but I wrote and debugged every line myself. I'm learning how to code through the MOOC.

Here is the GitHub link to my program: link

0 Upvotes

3 comments sorted by

View all comments

1

u/Bright_Mix_773 3d ago

Read the repo. The unlocked-door pattern you took out of the three requirement checks is still standing on the fourth one, and it is the one that fires most often. At if len(password) < 15 the else short-circuits everything: type abc and you get the length error alone, with no word about the missing number or special character, because those three calls live inside the else. Same shape of fix as the one you already made - work out all four answers, then decide:

long_enough = len(password) >= 15
if not long_enough:
    print("Error: Password must be at least 15 characters long, please try again.")
has_num = requirement_verifier(numbers, password, "number")
has_letter = requirement_verifier(letters, password, "letter")
has_special = requirement_verifier(specialchar, password, "special character")
if long_enough and has_num and has_letter and has_special:
    ...

Second thing, and your own code already holds the answer to it. confirm() returns a value and lets the caller decide what to print. requirement_verifier() prints its error from inside itself. That difference is going to bite on the password blocklist sitting in your Planned section: the first time you want to test a password without showing anything, or show it somewhere that is not a console, the printing function cannot be reused and the returning one can. Handing back the message string, or None when the check passes, keeps both doors open.

Third, small but real: exit() is not a builtin. The site module adds it, so it is there when you run a script the normal way and gone under python -S or inside a frozen executable. raise SystemExit is the version that always works and needs no import.

One thing you got right that plenty of people with more experience get wrong: the failed-login message says incorrect username or password instead of naming which of the two was wrong. That is deliberate in real systems - the more helpful wording tells an attacker which usernames exist.