creating a strong password [Python]










0















Need to create a function that validates my password strength having trouble with the third question.



  1. Both entered passwords are identical.

  2. The password is >= 8 characters in length.

3.If the first and last characters of the password are alphabetic then the first character of the password must be a different alphabetic letter to the last character of the password regardless of case. e.g 1st char is A so last char cannot be A or a.



def validate_password(first_pwd, second_pwd):
#Checks if password are same
if first_pwd == second_pwd:
#Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
#Checks if last and first character are alphabetic
if first_pwd[0].islower() and first_pwd[-1].isupper() or first_pwd[0].isupper() and first_pwd[-1].islower():
return True
else:
return False


print(validate_password("Abcd1234","Abcd1234"))


How do i ignore the digits at the end of the password and look at the nearest alphabetic letter which would be the d.










share|improve this question

















  • 1





    While this question has technically been answered... a far better approach would be to use an established library like zxcvbn-python if you want to actually check password strength. What you have here is extremely arbitrary and will allow such excellent passwords as 'aaaaaaab', 'password', '11111111', and 'username'.

    – kungphu
    Nov 13 '18 at 7:57












  • @mikuszefski You're attributing arguments to me that I did not make. All I did was point out how comically bad this specific idea of password strength is; unless you disagree with that, I don't think you and I have any disagreement.

    – kungphu
    Nov 13 '18 at 8:27











  • there are other attributes such as There are no more than 2 vowels in the password and The password has at least 1 alphabetic character in upper case and 1 alphabetic character in lower case. I just didn't feel the need to post them because they have been solved I was just having trouble with Question 3.

    – Mitchell Dehn
    Nov 13 '18 at 8:59






  • 1





    @kungphu true! Basically, my comment holds for the OP then. Cheers.

    – mikuszefski
    Nov 13 '18 at 9:15















0















Need to create a function that validates my password strength having trouble with the third question.



  1. Both entered passwords are identical.

  2. The password is >= 8 characters in length.

3.If the first and last characters of the password are alphabetic then the first character of the password must be a different alphabetic letter to the last character of the password regardless of case. e.g 1st char is A so last char cannot be A or a.



def validate_password(first_pwd, second_pwd):
#Checks if password are same
if first_pwd == second_pwd:
#Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
#Checks if last and first character are alphabetic
if first_pwd[0].islower() and first_pwd[-1].isupper() or first_pwd[0].isupper() and first_pwd[-1].islower():
return True
else:
return False


print(validate_password("Abcd1234","Abcd1234"))


How do i ignore the digits at the end of the password and look at the nearest alphabetic letter which would be the d.










share|improve this question

















  • 1





    While this question has technically been answered... a far better approach would be to use an established library like zxcvbn-python if you want to actually check password strength. What you have here is extremely arbitrary and will allow such excellent passwords as 'aaaaaaab', 'password', '11111111', and 'username'.

    – kungphu
    Nov 13 '18 at 7:57












  • @mikuszefski You're attributing arguments to me that I did not make. All I did was point out how comically bad this specific idea of password strength is; unless you disagree with that, I don't think you and I have any disagreement.

    – kungphu
    Nov 13 '18 at 8:27











  • there are other attributes such as There are no more than 2 vowels in the password and The password has at least 1 alphabetic character in upper case and 1 alphabetic character in lower case. I just didn't feel the need to post them because they have been solved I was just having trouble with Question 3.

    – Mitchell Dehn
    Nov 13 '18 at 8:59






  • 1





    @kungphu true! Basically, my comment holds for the OP then. Cheers.

    – mikuszefski
    Nov 13 '18 at 9:15













0












0








0








Need to create a function that validates my password strength having trouble with the third question.



  1. Both entered passwords are identical.

  2. The password is >= 8 characters in length.

3.If the first and last characters of the password are alphabetic then the first character of the password must be a different alphabetic letter to the last character of the password regardless of case. e.g 1st char is A so last char cannot be A or a.



def validate_password(first_pwd, second_pwd):
#Checks if password are same
if first_pwd == second_pwd:
#Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
#Checks if last and first character are alphabetic
if first_pwd[0].islower() and first_pwd[-1].isupper() or first_pwd[0].isupper() and first_pwd[-1].islower():
return True
else:
return False


print(validate_password("Abcd1234","Abcd1234"))


How do i ignore the digits at the end of the password and look at the nearest alphabetic letter which would be the d.










share|improve this question














Need to create a function that validates my password strength having trouble with the third question.



  1. Both entered passwords are identical.

  2. The password is >= 8 characters in length.

3.If the first and last characters of the password are alphabetic then the first character of the password must be a different alphabetic letter to the last character of the password regardless of case. e.g 1st char is A so last char cannot be A or a.



def validate_password(first_pwd, second_pwd):
#Checks if password are same
if first_pwd == second_pwd:
#Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
#Checks if last and first character are alphabetic
if first_pwd[0].islower() and first_pwd[-1].isupper() or first_pwd[0].isupper() and first_pwd[-1].islower():
return True
else:
return False


print(validate_password("Abcd1234","Abcd1234"))


How do i ignore the digits at the end of the password and look at the nearest alphabetic letter which would be the d.







python function






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 7:20









Mitchell DehnMitchell Dehn

133




133







  • 1





    While this question has technically been answered... a far better approach would be to use an established library like zxcvbn-python if you want to actually check password strength. What you have here is extremely arbitrary and will allow such excellent passwords as 'aaaaaaab', 'password', '11111111', and 'username'.

    – kungphu
    Nov 13 '18 at 7:57












  • @mikuszefski You're attributing arguments to me that I did not make. All I did was point out how comically bad this specific idea of password strength is; unless you disagree with that, I don't think you and I have any disagreement.

    – kungphu
    Nov 13 '18 at 8:27











  • there are other attributes such as There are no more than 2 vowels in the password and The password has at least 1 alphabetic character in upper case and 1 alphabetic character in lower case. I just didn't feel the need to post them because they have been solved I was just having trouble with Question 3.

    – Mitchell Dehn
    Nov 13 '18 at 8:59






  • 1





    @kungphu true! Basically, my comment holds for the OP then. Cheers.

    – mikuszefski
    Nov 13 '18 at 9:15












  • 1





    While this question has technically been answered... a far better approach would be to use an established library like zxcvbn-python if you want to actually check password strength. What you have here is extremely arbitrary and will allow such excellent passwords as 'aaaaaaab', 'password', '11111111', and 'username'.

    – kungphu
    Nov 13 '18 at 7:57












  • @mikuszefski You're attributing arguments to me that I did not make. All I did was point out how comically bad this specific idea of password strength is; unless you disagree with that, I don't think you and I have any disagreement.

    – kungphu
    Nov 13 '18 at 8:27











  • there are other attributes such as There are no more than 2 vowels in the password and The password has at least 1 alphabetic character in upper case and 1 alphabetic character in lower case. I just didn't feel the need to post them because they have been solved I was just having trouble with Question 3.

    – Mitchell Dehn
    Nov 13 '18 at 8:59






  • 1





    @kungphu true! Basically, my comment holds for the OP then. Cheers.

    – mikuszefski
    Nov 13 '18 at 9:15







1




1





While this question has technically been answered... a far better approach would be to use an established library like zxcvbn-python if you want to actually check password strength. What you have here is extremely arbitrary and will allow such excellent passwords as 'aaaaaaab', 'password', '11111111', and 'username'.

– kungphu
Nov 13 '18 at 7:57






While this question has technically been answered... a far better approach would be to use an established library like zxcvbn-python if you want to actually check password strength. What you have here is extremely arbitrary and will allow such excellent passwords as 'aaaaaaab', 'password', '11111111', and 'username'.

– kungphu
Nov 13 '18 at 7:57














@mikuszefski You're attributing arguments to me that I did not make. All I did was point out how comically bad this specific idea of password strength is; unless you disagree with that, I don't think you and I have any disagreement.

– kungphu
Nov 13 '18 at 8:27





@mikuszefski You're attributing arguments to me that I did not make. All I did was point out how comically bad this specific idea of password strength is; unless you disagree with that, I don't think you and I have any disagreement.

– kungphu
Nov 13 '18 at 8:27













there are other attributes such as There are no more than 2 vowels in the password and The password has at least 1 alphabetic character in upper case and 1 alphabetic character in lower case. I just didn't feel the need to post them because they have been solved I was just having trouble with Question 3.

– Mitchell Dehn
Nov 13 '18 at 8:59





there are other attributes such as There are no more than 2 vowels in the password and The password has at least 1 alphabetic character in upper case and 1 alphabetic character in lower case. I just didn't feel the need to post them because they have been solved I was just having trouble with Question 3.

– Mitchell Dehn
Nov 13 '18 at 8:59




1




1





@kungphu true! Basically, my comment holds for the OP then. Cheers.

– mikuszefski
Nov 13 '18 at 9:15





@kungphu true! Basically, my comment holds for the OP then. Cheers.

– mikuszefski
Nov 13 '18 at 9:15












2 Answers
2






active

oldest

votes


















0














You might try to solve it with a regex. This checks if the first occurrence of a letter equals (case-insensitively) the last occurrence of a letter.



You have to adapt the regex or the if clause if you really just want to check the first char of the string or the last one.

You also can do an extra check to see if the last char in the pwd equals the last letter in the list chars below.



import re

def validate_password(first_pwd, second_pwd):
# Checks if password are same
if first_pwd == second_pwd:
# Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
# get only the letters of the password
chars = re.findall(r'[A-Za-z]', first_pwd)
# compare first and last occurrence of letters in pwd
if chars[0].islower() and chars[-1].isupper() or chars[0].isupper() and chars[-1].islower():
return True
else:
return False


print(validate_password("Abcd1234","Abcd1234"))


I also think, it might be enough only to check



if chars[0].upper() == chars[-1].upper(): # optional if needed: and first_pwd[-1].upper() == chars[-1].upper()





share|improve this answer
































    0














    I'm not an expert, but as someone who tries to continually improve at python it's recommended to write out the words instead of using abbreviations.



    also you rother functions to verify the password strength are being checked only if the password was the same as the previous, so make sure you take length greater than 8 out of the first check



    Things worth looking at would be:



    islower, isalpha HERE



    with lower() HERE



    and I recommend youtubing regex because it's a tough concept imo



    import re

    def validate_password(first_password, second_password):
    if first_password == second_password:
    # Cannot be the previous password
    return False

    letters = "".join(re.findall("[a-zA-Z]+", first_password))
    first_letter = letters[0]
    last_letter = letters[-1]
    if len(first_password) >= 8:
    if first_letter.lower() == last_letter.lower():
    # First character can't be the same as last character
    return False
    else:
    # Every test passed
    return True
    else:
    # password too short
    return False

    print(validate_password("Abcd1234","Abcd1234"))





    share|improve this answer
























      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%2f53275777%2fcreating-a-strong-password-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









      0














      You might try to solve it with a regex. This checks if the first occurrence of a letter equals (case-insensitively) the last occurrence of a letter.



      You have to adapt the regex or the if clause if you really just want to check the first char of the string or the last one.

      You also can do an extra check to see if the last char in the pwd equals the last letter in the list chars below.



      import re

      def validate_password(first_pwd, second_pwd):
      # Checks if password are same
      if first_pwd == second_pwd:
      # Checks if password is greater or equal to 8 characters
      if len(first_pwd) >= 8:
      # get only the letters of the password
      chars = re.findall(r'[A-Za-z]', first_pwd)
      # compare first and last occurrence of letters in pwd
      if chars[0].islower() and chars[-1].isupper() or chars[0].isupper() and chars[-1].islower():
      return True
      else:
      return False


      print(validate_password("Abcd1234","Abcd1234"))


      I also think, it might be enough only to check



      if chars[0].upper() == chars[-1].upper(): # optional if needed: and first_pwd[-1].upper() == chars[-1].upper()





      share|improve this answer





























        0














        You might try to solve it with a regex. This checks if the first occurrence of a letter equals (case-insensitively) the last occurrence of a letter.



        You have to adapt the regex or the if clause if you really just want to check the first char of the string or the last one.

        You also can do an extra check to see if the last char in the pwd equals the last letter in the list chars below.



        import re

        def validate_password(first_pwd, second_pwd):
        # Checks if password are same
        if first_pwd == second_pwd:
        # Checks if password is greater or equal to 8 characters
        if len(first_pwd) >= 8:
        # get only the letters of the password
        chars = re.findall(r'[A-Za-z]', first_pwd)
        # compare first and last occurrence of letters in pwd
        if chars[0].islower() and chars[-1].isupper() or chars[0].isupper() and chars[-1].islower():
        return True
        else:
        return False


        print(validate_password("Abcd1234","Abcd1234"))


        I also think, it might be enough only to check



        if chars[0].upper() == chars[-1].upper(): # optional if needed: and first_pwd[-1].upper() == chars[-1].upper()





        share|improve this answer



























          0












          0








          0







          You might try to solve it with a regex. This checks if the first occurrence of a letter equals (case-insensitively) the last occurrence of a letter.



          You have to adapt the regex or the if clause if you really just want to check the first char of the string or the last one.

          You also can do an extra check to see if the last char in the pwd equals the last letter in the list chars below.



          import re

          def validate_password(first_pwd, second_pwd):
          # Checks if password are same
          if first_pwd == second_pwd:
          # Checks if password is greater or equal to 8 characters
          if len(first_pwd) >= 8:
          # get only the letters of the password
          chars = re.findall(r'[A-Za-z]', first_pwd)
          # compare first and last occurrence of letters in pwd
          if chars[0].islower() and chars[-1].isupper() or chars[0].isupper() and chars[-1].islower():
          return True
          else:
          return False


          print(validate_password("Abcd1234","Abcd1234"))


          I also think, it might be enough only to check



          if chars[0].upper() == chars[-1].upper(): # optional if needed: and first_pwd[-1].upper() == chars[-1].upper()





          share|improve this answer















          You might try to solve it with a regex. This checks if the first occurrence of a letter equals (case-insensitively) the last occurrence of a letter.



          You have to adapt the regex or the if clause if you really just want to check the first char of the string or the last one.

          You also can do an extra check to see if the last char in the pwd equals the last letter in the list chars below.



          import re

          def validate_password(first_pwd, second_pwd):
          # Checks if password are same
          if first_pwd == second_pwd:
          # Checks if password is greater or equal to 8 characters
          if len(first_pwd) >= 8:
          # get only the letters of the password
          chars = re.findall(r'[A-Za-z]', first_pwd)
          # compare first and last occurrence of letters in pwd
          if chars[0].islower() and chars[-1].isupper() or chars[0].isupper() and chars[-1].islower():
          return True
          else:
          return False


          print(validate_password("Abcd1234","Abcd1234"))


          I also think, it might be enough only to check



          if chars[0].upper() == chars[-1].upper(): # optional if needed: and first_pwd[-1].upper() == chars[-1].upper()






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 7:58

























          answered Nov 13 '18 at 7:51









          zyprozypro

          6352828




          6352828























              0














              I'm not an expert, but as someone who tries to continually improve at python it's recommended to write out the words instead of using abbreviations.



              also you rother functions to verify the password strength are being checked only if the password was the same as the previous, so make sure you take length greater than 8 out of the first check



              Things worth looking at would be:



              islower, isalpha HERE



              with lower() HERE



              and I recommend youtubing regex because it's a tough concept imo



              import re

              def validate_password(first_password, second_password):
              if first_password == second_password:
              # Cannot be the previous password
              return False

              letters = "".join(re.findall("[a-zA-Z]+", first_password))
              first_letter = letters[0]
              last_letter = letters[-1]
              if len(first_password) >= 8:
              if first_letter.lower() == last_letter.lower():
              # First character can't be the same as last character
              return False
              else:
              # Every test passed
              return True
              else:
              # password too short
              return False

              print(validate_password("Abcd1234","Abcd1234"))





              share|improve this answer





























                0














                I'm not an expert, but as someone who tries to continually improve at python it's recommended to write out the words instead of using abbreviations.



                also you rother functions to verify the password strength are being checked only if the password was the same as the previous, so make sure you take length greater than 8 out of the first check



                Things worth looking at would be:



                islower, isalpha HERE



                with lower() HERE



                and I recommend youtubing regex because it's a tough concept imo



                import re

                def validate_password(first_password, second_password):
                if first_password == second_password:
                # Cannot be the previous password
                return False

                letters = "".join(re.findall("[a-zA-Z]+", first_password))
                first_letter = letters[0]
                last_letter = letters[-1]
                if len(first_password) >= 8:
                if first_letter.lower() == last_letter.lower():
                # First character can't be the same as last character
                return False
                else:
                # Every test passed
                return True
                else:
                # password too short
                return False

                print(validate_password("Abcd1234","Abcd1234"))





                share|improve this answer



























                  0












                  0








                  0







                  I'm not an expert, but as someone who tries to continually improve at python it's recommended to write out the words instead of using abbreviations.



                  also you rother functions to verify the password strength are being checked only if the password was the same as the previous, so make sure you take length greater than 8 out of the first check



                  Things worth looking at would be:



                  islower, isalpha HERE



                  with lower() HERE



                  and I recommend youtubing regex because it's a tough concept imo



                  import re

                  def validate_password(first_password, second_password):
                  if first_password == second_password:
                  # Cannot be the previous password
                  return False

                  letters = "".join(re.findall("[a-zA-Z]+", first_password))
                  first_letter = letters[0]
                  last_letter = letters[-1]
                  if len(first_password) >= 8:
                  if first_letter.lower() == last_letter.lower():
                  # First character can't be the same as last character
                  return False
                  else:
                  # Every test passed
                  return True
                  else:
                  # password too short
                  return False

                  print(validate_password("Abcd1234","Abcd1234"))





                  share|improve this answer















                  I'm not an expert, but as someone who tries to continually improve at python it's recommended to write out the words instead of using abbreviations.



                  also you rother functions to verify the password strength are being checked only if the password was the same as the previous, so make sure you take length greater than 8 out of the first check



                  Things worth looking at would be:



                  islower, isalpha HERE



                  with lower() HERE



                  and I recommend youtubing regex because it's a tough concept imo



                  import re

                  def validate_password(first_password, second_password):
                  if first_password == second_password:
                  # Cannot be the previous password
                  return False

                  letters = "".join(re.findall("[a-zA-Z]+", first_password))
                  first_letter = letters[0]
                  last_letter = letters[-1]
                  if len(first_password) >= 8:
                  if first_letter.lower() == last_letter.lower():
                  # First character can't be the same as last character
                  return False
                  else:
                  # Every test passed
                  return True
                  else:
                  # password too short
                  return False

                  print(validate_password("Abcd1234","Abcd1234"))






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 '18 at 8:13

























                  answered Nov 13 '18 at 7:53









                  DavidhallDavidhall

                  259




                  259



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275777%2fcreating-a-strong-password-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







                      Popular posts from this blog

                      𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                      How do I collapse sections of code in Visual Studio Code for Windows?

                      ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ