r/PythonProjects2 • u/Senior-Locksmith-945 • Aug 15 '25
I'm currently developing a PIN Verification System as a Python beginner so I need some feedback to improve.
54
Upvotes
r/PythonProjects2 • u/Senior-Locksmith-945 • Aug 15 '25
7
u/JustAnotherTeapot418 Aug 15 '25 edited Aug 15 '25
max_attemptsseems to be a constant, so I would call itMAX_ATTEMPTS(all uppercase) to indicate it's a constant and isn't meant to change at runtime.You probably don't want to prompt
employee_namefrom within the loop. As a user, I wouldn't want to type "Bob" multiple times when all I did was mistype my PIN.Consider whether you truly want
employee_nameto be case-sensitive. Bob should be allowed to login as "bob", "BOB", "Bob", etc. Currently, he can only login as "Bob". You can fix this by defining your dictionary with"Bob".upper()and checking foremployee_name.upper()for example. If you're using Python 3.3, you can do"Bob".casefold()andemployee_name.casefold()instead.You might want to use something other than
input()to get the PIN, as it'll show the PIN on screen. This is a security flaw, as any onlooker could easily steal the PIN just from looking at your screen. Consider using getpass.If you move
attempts += 1to the start of the loop, you can save 1 line of code. It also allows you to usecontinue(i.e. "if FAIL then print FAIL and continue") and avoidelsein order to reduce the amount of code nesting (this makes the code easier to read). Since this would break the finalattempts >= max_attemptscheck (it would trigger if you succeed on your 3rd try), consider using ais_login_successboolean instead.To make the code even easier to read, you can move the PIN request part into its own
def prompt_pin(user)function. That way you could dowhile attempts < max_attempts and not prompt_pin(employee_name). Then you wouldn't need theis_login_successboolean from point 4.From a security point of view, you shouldn't tell the user that
"Employee name not found"as it helps attackers figure out what names can be brute-forced and what names can't. Just pretend the username exists and simply tell the user that the "login failed" (with no additional info on what failed).From a security point of view, you shouldn't rely on
pin == entered_pin. The reason being that this is likely optimized as "if current char don't match, return False, else continue with next char". The reason this is unsafe is because an attacker could try and measure how much time passed between sending the PIN and getting the "login failed" message. The longer it takes, the more characters are correct. This is known as a side-channel attack.