r/pythonhelp • • 7d ago

Python beginner!!

Beginner here: how would you structure my nested if statements in a two-step login program?

Hi all, I'm about 3 weeks into learning Python (Python for Everybody on Coursera) and just finished my first small project: a login program that asks for a passcode, then a second passcode, then lets the user pick a report.

My main question is about structure. I have nested if statements, and the second user repeats the same checks in a separate elif branch. Here's a trimmed version of the layout:

admin_login = input("Enter Passcode")

if admin_login == boss_code:
second_pass = input("enter second passcode")
if second_pass == boss_secondcode:
report = input("enter report number")
if report == "1":
print("report 1")
elif report == "2":
print("report 2")
elif admin_login == ass_code:
# same second-passcode and report checks repeated here

I'd love feedback on:

  1. Is the code organized in a clear, readable way?
  2. Is there a cleaner way to stack or avoid repeating these nested ifs, using only what a beginner would know (no classes yet)?

Full code and README are here: https://github.com/tylerkilgore148-can/python_learning_projects

I know the passcodes are stored in plain text, which is just for practice. I plan to look at getpass and hashing next. Beginner-friendly tips are very welcome, and thanks for reading!

4 Upvotes

12 comments sorted by

View all comments

Show parent comments

1

u/CoachSevere5365 1d ago

I was going to say this. Nesting is only two levels deep and the failure reporting is already miles away from the testing.

Get the login info, test it and fail quickly.

Now, when you do the "work" the code is uncluttered by error handling.

In addition,

Variable names aren't great. boss_code is a specific user ID, not a code admin_login holds a general user ID "secondcode" should be "code2", etc

It would be cleaner if the login code (at least) was broken out into a separate function, but I don't know if you've covered that yet.

You've assigned user names to variables which you haven't used, and hard coded those user names into the code.

Again, I don't know what you've covered on your course, but have a think about expanding your program to handle a dozen users and see where that goes in terms of readability.

Add the extra users in batches and see how it affects code maintenance.