Search code examples
pythonlistfunctionparsingspecial-characters

Can someone please fix it so that the function can detect whether the input has at least one symbol in it?


import os
import time

def strong(password, verifier):
  symbols = "! # $ % & ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~"
  password = str(password)
  if len(password) > 8:
    if len(password) < 15:
        for i in symbols:
          if password.find(symbols) is True:

I want to see if I can fix this part because I am not sure how I can use this function to find a special character in the password.

            if password.isalnum():
              verifier = 1
              return 'good password', verifier
            elif password.isalpha():
              return 'Your password is only letters'
            elif password.isnumeric():
              return 'Your password is only numbers'
          else:
            return 'no symbols'
    else:
      return 'Too big'
  else:
    return 'Too small'

Solution

  • You can simplify this (and make debugging easier) by just returning after each failed check instead of having all the deeply nested if/else. Since in the "good password" case you're returning a "verifier" as well as the "good password" message, I'm figuring you probably want to return a false value for the "bad password" cases as well:

    def is_strong(password):
        symbols = set("!#$%&()*+,-./:;=?@[]^_`{|}~")
        if len(password) < 9:
            return "Too small", False
        if len(password) > 14:
            return "Too big", False
        if not any(c in symbols for c in password):
            return "No symbols", False
        if password.isalpha():
            return "Your password is only letters", False
        if password.isnumeric():
            return "your password is only numbers", False
        return "Good password", True
    

    Once the function is simplified down, we can see that the isalpha and isnumeric checks won't actually do anything, because we've already established that the password contains non-alphanumeric symbols! Instead I think you want to do:

    def is_strong(password):
        symbols = set("!#$%&()*+,-./:;=?@[]^_`{|}~")
        if len(password) < 9:
            return "Too small", False
        if len(password) > 14:
            return "Too big", False
        if not any(c in symbols for c in password):
            return "No symbols", False
        if not any(c.isalpha() for c in password):
            return "No letters", False
        if not any(c.isdigit() for c in password):
            return "No numbers", False
        return "Good password", True
    

    We can test the function like this:

    strong, password = False, ""
    while not strong:
        password = input("Set password: ")
        msg, strong = is_strong(password)
        print(msg)
    
    Set password: bob
    Too small
    Set password: bobledeboingerberoo
    Too big
    Set password: bobbington
    No symbols
    Set password: bobbington!
    No numbers
    Set password: bobbington3!
    Good password