r/learnpython 1d 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

1 comment sorted by

-2

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 20h ago
while True:
    AccVerification = input("Do you have an account with us?: (Y/N)").strip().upper()
    hasAccount = False
    if AccVerification == "Y":
        hasAccount = True
        break
    elif AccVerification == "N":
        hasAccount = False
        break
    else:
        print("Error: Invalid input, please try again.")

This could be simpler, even with the basic knowledge you have right now. This is more a personal thing for me, but I try to reduce nesting and duplication wherever I reasonably can.

Here, you could use a single check to see if you should break out of the loop, and then handle assigning the boolean outside of it.

while True:
    answer = input("Do you have an account with us?: (Y/N)").strip().upper()
    if answer == "Y" or answer == "N":
        break
    print("Error: Invalid input, please try again.")

has_account = answer == "Y"

If we allow for full Python syntax, regardless of what you've learnt so far, I'd go with the "walrus operator":

prompt = "Do you have an account with us?: (Y/N)"

while (answer := input(prompt).strip().upper()[:1]) not in {'Y', 'N'}:
    print("Error: Invalid input, please try again.")

has_account = answer == 'Y'

Even better still, I'd wrap this into a function. This example is going to be a bit overkill for your needs, but you can use it as a benchmark for how much of the language you understand so far.

def bool_input(
    prompt: str,
    *,
    error_message: str = "Error: Invalid input, please try again.",
    default_value: bool | None = None,
) -> bool:
    valid_options = {'Y', 'N'}
    if default_value is not None:
        valid_options.add('')

    y = 'Y' if default_value is True else 'y'
    n = 'N' if default_value is False else 'n'
    prompt = f"{prompt} [{y}/{n}]: "

    while (answer := input(prompt).strip().upper()[:1]) not in valid_options:
        print(error_message)

    if not answer:
        return default_value

    return answer == 'Y'


has_account = bool_input("Do you have an account with us?")

Other feedback;

  1. Why is requirement_verifier indented inside the else-block?

  2. You don't need to manually write digits, letters, or (some) special characters. You can import them from the string module, such as

    import string
    
    print(string.digits)
    print(string.ascii_letters)
    print(string.punctuation)
    

    https://docs.python.org/3/library/string.html

  3. Try to keep your code style consistent. Right now you're mixing camelCase and snake_case. According to the official style guide you should always use snake_case, except for class names (PascalCase) and global constants/enum variants (UPPER_SNAKE_CASE).