#๐Ÿ”’ Is this code good now and if i can how can i make it better

26 messages ยท Page 1 of 1 (latest)

silk plaza
#

specials = "!ยฃ$%^&*()?><@#"
while True:
good_password = True

password = input("Enter your password: ") 
if len(password)<8:
    print("Minimum 8 Characters") 
    good_password = False
    
if not any(char in specials for char in password): 
    print("Include a Special Character")
    good_password = False

if not any(char.isupper() for char in password): 
    print("Include a Capital Letter")
    good_password = False

if not any(char.islower() for char in password): 
    print("Include a Lowercase Letter")
    good_password = False
    
if not any(char.isdigit()for char in password):
    print("Include a Number")
    good_password = False

if good_password:
    print("thats a secure password!")
    break
viscid oysterBOT
#

Hey @silk plaza!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
viscid oysterBOT
#

@silk plaza

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

inland matrix
#

u could use matvh case for cleaner code ig, and instead of making a variable good password u just do the match _ i think which matches if nothing else does i think

#

wait nvm

plain cape
#

great use of any and generator expressions.

inland matrix
#

why not use elif and else?

plain cape
#

that would have different behavior.

inland matrix
#

if a:
print bad case a
elif b
print bad case b
else
print success
break

plain cape
#

and it would be worse. users would rather know all the ways they fucked up all at once. not find them out one-by-one.

inland matrix
#

but youre right ig

plain cape
cerulean flare
# inland matrix haha all the websites say it one by one lol

which website?
i haven't seen any website in connection to these posts
however, in one of the previous posts about this code, OP specifically expressed that they wanted all applicable problems with the password to be displayed before prompting for a new password again

dusty sable
#

Length 8 is also on the "not so save anymore" side now.

cerulean flare
inland matrix
#

one thing you could add is just check using regex at the start if it works, and if it doesnt, then go through the if statements. that way theres no need for good_password variable

#

if regex: break
if case a: print fail a
if case b: print fail b
etc

spice phoenix
#

One alternative could be something like this, but this might just be overengineering it:

specials = "!ยฃ$%^&*()?><@#"


@dataclass
class Validation:
    validator: Callable[[str], bool]
    error: str


validations: list[Validation] = [
    Validation(lambda s: len(s) >= 8, "Minimum 8 Characters"),
    Validation(
        lambda s: any(ch in specials for ch in s), "Include a Special Character"
    ),
    Validation(lambda s: any(ch.isupper() for ch in s), "Include a Capital Letter"),
    Validation(lambda s: any(ch.islower() for ch in s), "Include a Lowercase Letter"),
    Validation(lambda s: any(ch.isdigit() for ch in s), "Include a Number"),
]

while True:
    password = input("Enter your password: ")

    errors = [v.error for v in validations if not v.validator(password)]

    if errors:
        for msg in errors:
            print(msg)
        continue

    print("thats a secure password!")
    break
limpid galleon
#

The most performant and (IMO) most graceful approach would be a large regex statement which did multiple lookaheads to test multiple conditions on the same string, then a series of if statements (not if-else, since you want per-error reports instead of a ladder).

#

You should also be using the string library for tracking things like specials, ascii vs non-ascii

viscid oysterBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.