Test the Strength of a Password in Python












1














A string is a WEAK password if:
either, it is less than 8 characters long,
or, it is an English word,which is function is_english_word( ) is True.



A string is a STRONG password if:
it contains at least 11 characters
AND it contains at least 1 lower case letter
AND it contains at least 1 capital letter
AND it contains at least 1 numerical digit.



A string is a MEDIUM password if it is NOT a WEAK password AND is NOT a STRONG password.



def is_english_word( string ):
with open("english_words.txt") as f:
word_list =
for line in f.readlines():
word_list.append(line.strip())
if string in word_list:
return True
elif string == string.upper() and string.lower() in word_list:
return True
elif string == string.title() and string.lower() in word_list:
return True
else:
return False

def password_strength( string ):
lower = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
upper = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
for item in string:
if item in lower:
string = string.replace(item, "x")
elif item in upper:
string = string.replace(item, "y")
elif item.isnumeric():
string = string.replace(item, "n")
for item in string:
if len( string ) < 8 or is_english_word( string ) :
return 'WEAK'
elif len( string ) >= 11 and string.count("x") >= 1 and string.count("y") >= 1 and string.count("n") >= 1:
return 'STRONG'
else:
return 'MEDIUM'

print( password_strength( 'Unimaginatively' ) )


This password should be "WEAK",but the output is "MEDIUM",I don't know what's the problem of my codes.Many thanks.










share|improve this question



























    1














    A string is a WEAK password if:
    either, it is less than 8 characters long,
    or, it is an English word,which is function is_english_word( ) is True.



    A string is a STRONG password if:
    it contains at least 11 characters
    AND it contains at least 1 lower case letter
    AND it contains at least 1 capital letter
    AND it contains at least 1 numerical digit.



    A string is a MEDIUM password if it is NOT a WEAK password AND is NOT a STRONG password.



    def is_english_word( string ):
    with open("english_words.txt") as f:
    word_list =
    for line in f.readlines():
    word_list.append(line.strip())
    if string in word_list:
    return True
    elif string == string.upper() and string.lower() in word_list:
    return True
    elif string == string.title() and string.lower() in word_list:
    return True
    else:
    return False

    def password_strength( string ):
    lower = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
    upper = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
    for item in string:
    if item in lower:
    string = string.replace(item, "x")
    elif item in upper:
    string = string.replace(item, "y")
    elif item.isnumeric():
    string = string.replace(item, "n")
    for item in string:
    if len( string ) < 8 or is_english_word( string ) :
    return 'WEAK'
    elif len( string ) >= 11 and string.count("x") >= 1 and string.count("y") >= 1 and string.count("n") >= 1:
    return 'STRONG'
    else:
    return 'MEDIUM'

    print( password_strength( 'Unimaginatively' ) )


    This password should be "WEAK",but the output is "MEDIUM",I don't know what's the problem of my codes.Many thanks.










    share|improve this question

























      1












      1








      1







      A string is a WEAK password if:
      either, it is less than 8 characters long,
      or, it is an English word,which is function is_english_word( ) is True.



      A string is a STRONG password if:
      it contains at least 11 characters
      AND it contains at least 1 lower case letter
      AND it contains at least 1 capital letter
      AND it contains at least 1 numerical digit.



      A string is a MEDIUM password if it is NOT a WEAK password AND is NOT a STRONG password.



      def is_english_word( string ):
      with open("english_words.txt") as f:
      word_list =
      for line in f.readlines():
      word_list.append(line.strip())
      if string in word_list:
      return True
      elif string == string.upper() and string.lower() in word_list:
      return True
      elif string == string.title() and string.lower() in word_list:
      return True
      else:
      return False

      def password_strength( string ):
      lower = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
      upper = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
      for item in string:
      if item in lower:
      string = string.replace(item, "x")
      elif item in upper:
      string = string.replace(item, "y")
      elif item.isnumeric():
      string = string.replace(item, "n")
      for item in string:
      if len( string ) < 8 or is_english_word( string ) :
      return 'WEAK'
      elif len( string ) >= 11 and string.count("x") >= 1 and string.count("y") >= 1 and string.count("n") >= 1:
      return 'STRONG'
      else:
      return 'MEDIUM'

      print( password_strength( 'Unimaginatively' ) )


      This password should be "WEAK",but the output is "MEDIUM",I don't know what's the problem of my codes.Many thanks.










      share|improve this question













      A string is a WEAK password if:
      either, it is less than 8 characters long,
      or, it is an English word,which is function is_english_word( ) is True.



      A string is a STRONG password if:
      it contains at least 11 characters
      AND it contains at least 1 lower case letter
      AND it contains at least 1 capital letter
      AND it contains at least 1 numerical digit.



      A string is a MEDIUM password if it is NOT a WEAK password AND is NOT a STRONG password.



      def is_english_word( string ):
      with open("english_words.txt") as f:
      word_list =
      for line in f.readlines():
      word_list.append(line.strip())
      if string in word_list:
      return True
      elif string == string.upper() and string.lower() in word_list:
      return True
      elif string == string.title() and string.lower() in word_list:
      return True
      else:
      return False

      def password_strength( string ):
      lower = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
      upper = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
      for item in string:
      if item in lower:
      string = string.replace(item, "x")
      elif item in upper:
      string = string.replace(item, "y")
      elif item.isnumeric():
      string = string.replace(item, "n")
      for item in string:
      if len( string ) < 8 or is_english_word( string ) :
      return 'WEAK'
      elif len( string ) >= 11 and string.count("x") >= 1 and string.count("y") >= 1 and string.count("n") >= 1:
      return 'STRONG'
      else:
      return 'MEDIUM'

      print( password_strength( 'Unimaginatively' ) )


      This password should be "WEAK",but the output is "MEDIUM",I don't know what's the problem of my codes.Many thanks.







      python python-3.x data-science data-analysis






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 21:26









      CeciliaCecilia

      587




      587
























          2 Answers
          2






          active

          oldest

          votes


















          3














          There are a number of problems with your code; Notably, you are replacing the lowercase chars with x, uppercase with y and digits with n, before calling is_english_word - that means is_english_word() will be called with 'Xyyyyyyyyyyyyyy' which is not an english word. That is making your password not 'WEAK'.



          Since it is also not 'STRONG', it ends up being 'MEDIUM'.



          For the record, here is an example of a correct code to do what you want:



          import string
          def password_strength(string):
          if len(string) < 8 or is_english_word(string):
          return 'WEAK'
          elif (len(string) > 11 and
          any(ch in string.ascii_lowercase for ch in string) and
          any(ch in string.ascii_uppercase for ch in string) and
          any(ch.isdigit() for ch in string)):
          return 'STRONG'
          else:
          return 'MEDIUM'





          share|improve this answer































            2














            If you think of those 3 restrictions on the types of letters in terms of the same operation (checking it is true for at least one character) performed using string methods, you can simplify your code significantly:



            def is_weak(word):
            return len(word < 8 or is_english_word(word)

            def is_strong(word):
            return len(word) >= 11 and all(any(method(c) for c in word)
            for method in (str.islower, str.isupper, str.isdigit))

            def password_strength(password):
            if is_weak(password):
            return 'WEAK'
            elif is_strong(password):
            return 'STRONG'
            else:
            return 'MEDIUM'





            share|improve this answer





















            • Thanks for the answer,could you please tell me the format of this code'all(any(method(c) for c in word) for method in (str.islower, str.isupper, str.isdigit))',is it in one line?
              – Cecilia
              Nov 12 '18 at 21:52










            • @Cecilia Yes, I just split it across lines for readability. If you have an expression inside brackets ({}, , or ()), you can have line breaks in the middle, and it will still be interpreted as a single expression.
              – Patrick Haugh
              Nov 12 '18 at 21:56










            • Got it!Thank you so much
              – Cecilia
              Nov 12 '18 at 22:07











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270357%2ftest-the-strength-of-a-password-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            There are a number of problems with your code; Notably, you are replacing the lowercase chars with x, uppercase with y and digits with n, before calling is_english_word - that means is_english_word() will be called with 'Xyyyyyyyyyyyyyy' which is not an english word. That is making your password not 'WEAK'.



            Since it is also not 'STRONG', it ends up being 'MEDIUM'.



            For the record, here is an example of a correct code to do what you want:



            import string
            def password_strength(string):
            if len(string) < 8 or is_english_word(string):
            return 'WEAK'
            elif (len(string) > 11 and
            any(ch in string.ascii_lowercase for ch in string) and
            any(ch in string.ascii_uppercase for ch in string) and
            any(ch.isdigit() for ch in string)):
            return 'STRONG'
            else:
            return 'MEDIUM'





            share|improve this answer




























              3














              There are a number of problems with your code; Notably, you are replacing the lowercase chars with x, uppercase with y and digits with n, before calling is_english_word - that means is_english_word() will be called with 'Xyyyyyyyyyyyyyy' which is not an english word. That is making your password not 'WEAK'.



              Since it is also not 'STRONG', it ends up being 'MEDIUM'.



              For the record, here is an example of a correct code to do what you want:



              import string
              def password_strength(string):
              if len(string) < 8 or is_english_word(string):
              return 'WEAK'
              elif (len(string) > 11 and
              any(ch in string.ascii_lowercase for ch in string) and
              any(ch in string.ascii_uppercase for ch in string) and
              any(ch.isdigit() for ch in string)):
              return 'STRONG'
              else:
              return 'MEDIUM'





              share|improve this answer


























                3












                3








                3






                There are a number of problems with your code; Notably, you are replacing the lowercase chars with x, uppercase with y and digits with n, before calling is_english_word - that means is_english_word() will be called with 'Xyyyyyyyyyyyyyy' which is not an english word. That is making your password not 'WEAK'.



                Since it is also not 'STRONG', it ends up being 'MEDIUM'.



                For the record, here is an example of a correct code to do what you want:



                import string
                def password_strength(string):
                if len(string) < 8 or is_english_word(string):
                return 'WEAK'
                elif (len(string) > 11 and
                any(ch in string.ascii_lowercase for ch in string) and
                any(ch in string.ascii_uppercase for ch in string) and
                any(ch.isdigit() for ch in string)):
                return 'STRONG'
                else:
                return 'MEDIUM'





                share|improve this answer














                There are a number of problems with your code; Notably, you are replacing the lowercase chars with x, uppercase with y and digits with n, before calling is_english_word - that means is_english_word() will be called with 'Xyyyyyyyyyyyyyy' which is not an english word. That is making your password not 'WEAK'.



                Since it is also not 'STRONG', it ends up being 'MEDIUM'.



                For the record, here is an example of a correct code to do what you want:



                import string
                def password_strength(string):
                if len(string) < 8 or is_english_word(string):
                return 'WEAK'
                elif (len(string) > 11 and
                any(ch in string.ascii_lowercase for ch in string) and
                any(ch in string.ascii_uppercase for ch in string) and
                any(ch.isdigit() for ch in string)):
                return 'STRONG'
                else:
                return 'MEDIUM'






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 '18 at 21:39

























                answered Nov 12 '18 at 21:33









                nosklonosklo

                152k46249272




                152k46249272

























                    2














                    If you think of those 3 restrictions on the types of letters in terms of the same operation (checking it is true for at least one character) performed using string methods, you can simplify your code significantly:



                    def is_weak(word):
                    return len(word < 8 or is_english_word(word)

                    def is_strong(word):
                    return len(word) >= 11 and all(any(method(c) for c in word)
                    for method in (str.islower, str.isupper, str.isdigit))

                    def password_strength(password):
                    if is_weak(password):
                    return 'WEAK'
                    elif is_strong(password):
                    return 'STRONG'
                    else:
                    return 'MEDIUM'





                    share|improve this answer





















                    • Thanks for the answer,could you please tell me the format of this code'all(any(method(c) for c in word) for method in (str.islower, str.isupper, str.isdigit))',is it in one line?
                      – Cecilia
                      Nov 12 '18 at 21:52










                    • @Cecilia Yes, I just split it across lines for readability. If you have an expression inside brackets ({}, , or ()), you can have line breaks in the middle, and it will still be interpreted as a single expression.
                      – Patrick Haugh
                      Nov 12 '18 at 21:56










                    • Got it!Thank you so much
                      – Cecilia
                      Nov 12 '18 at 22:07
















                    2














                    If you think of those 3 restrictions on the types of letters in terms of the same operation (checking it is true for at least one character) performed using string methods, you can simplify your code significantly:



                    def is_weak(word):
                    return len(word < 8 or is_english_word(word)

                    def is_strong(word):
                    return len(word) >= 11 and all(any(method(c) for c in word)
                    for method in (str.islower, str.isupper, str.isdigit))

                    def password_strength(password):
                    if is_weak(password):
                    return 'WEAK'
                    elif is_strong(password):
                    return 'STRONG'
                    else:
                    return 'MEDIUM'





                    share|improve this answer





















                    • Thanks for the answer,could you please tell me the format of this code'all(any(method(c) for c in word) for method in (str.islower, str.isupper, str.isdigit))',is it in one line?
                      – Cecilia
                      Nov 12 '18 at 21:52










                    • @Cecilia Yes, I just split it across lines for readability. If you have an expression inside brackets ({}, , or ()), you can have line breaks in the middle, and it will still be interpreted as a single expression.
                      – Patrick Haugh
                      Nov 12 '18 at 21:56










                    • Got it!Thank you so much
                      – Cecilia
                      Nov 12 '18 at 22:07














                    2












                    2








                    2






                    If you think of those 3 restrictions on the types of letters in terms of the same operation (checking it is true for at least one character) performed using string methods, you can simplify your code significantly:



                    def is_weak(word):
                    return len(word < 8 or is_english_word(word)

                    def is_strong(word):
                    return len(word) >= 11 and all(any(method(c) for c in word)
                    for method in (str.islower, str.isupper, str.isdigit))

                    def password_strength(password):
                    if is_weak(password):
                    return 'WEAK'
                    elif is_strong(password):
                    return 'STRONG'
                    else:
                    return 'MEDIUM'





                    share|improve this answer












                    If you think of those 3 restrictions on the types of letters in terms of the same operation (checking it is true for at least one character) performed using string methods, you can simplify your code significantly:



                    def is_weak(word):
                    return len(word < 8 or is_english_word(word)

                    def is_strong(word):
                    return len(word) >= 11 and all(any(method(c) for c in word)
                    for method in (str.islower, str.isupper, str.isdigit))

                    def password_strength(password):
                    if is_weak(password):
                    return 'WEAK'
                    elif is_strong(password):
                    return 'STRONG'
                    else:
                    return 'MEDIUM'






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 '18 at 21:44









                    Patrick HaughPatrick Haugh

                    27.7k82546




                    27.7k82546












                    • Thanks for the answer,could you please tell me the format of this code'all(any(method(c) for c in word) for method in (str.islower, str.isupper, str.isdigit))',is it in one line?
                      – Cecilia
                      Nov 12 '18 at 21:52










                    • @Cecilia Yes, I just split it across lines for readability. If you have an expression inside brackets ({}, , or ()), you can have line breaks in the middle, and it will still be interpreted as a single expression.
                      – Patrick Haugh
                      Nov 12 '18 at 21:56










                    • Got it!Thank you so much
                      – Cecilia
                      Nov 12 '18 at 22:07


















                    • Thanks for the answer,could you please tell me the format of this code'all(any(method(c) for c in word) for method in (str.islower, str.isupper, str.isdigit))',is it in one line?
                      – Cecilia
                      Nov 12 '18 at 21:52










                    • @Cecilia Yes, I just split it across lines for readability. If you have an expression inside brackets ({}, , or ()), you can have line breaks in the middle, and it will still be interpreted as a single expression.
                      – Patrick Haugh
                      Nov 12 '18 at 21:56










                    • Got it!Thank you so much
                      – Cecilia
                      Nov 12 '18 at 22:07
















                    Thanks for the answer,could you please tell me the format of this code'all(any(method(c) for c in word) for method in (str.islower, str.isupper, str.isdigit))',is it in one line?
                    – Cecilia
                    Nov 12 '18 at 21:52




                    Thanks for the answer,could you please tell me the format of this code'all(any(method(c) for c in word) for method in (str.islower, str.isupper, str.isdigit))',is it in one line?
                    – Cecilia
                    Nov 12 '18 at 21:52












                    @Cecilia Yes, I just split it across lines for readability. If you have an expression inside brackets ({}, , or ()), you can have line breaks in the middle, and it will still be interpreted as a single expression.
                    – Patrick Haugh
                    Nov 12 '18 at 21:56




                    @Cecilia Yes, I just split it across lines for readability. If you have an expression inside brackets ({}, , or ()), you can have line breaks in the middle, and it will still be interpreted as a single expression.
                    – Patrick Haugh
                    Nov 12 '18 at 21:56












                    Got it!Thank you so much
                    – Cecilia
                    Nov 12 '18 at 22:07




                    Got it!Thank you so much
                    – Cecilia
                    Nov 12 '18 at 22:07


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270357%2ftest-the-strength-of-a-password-in-python%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()