3
\$\begingroup\$

I am creating a code which checks a user inputted password and gives points based on it. One of the points is: If it has 3 letters adjacent/consecutive on the QWERTY UK keyboard then it will be -5 points. e.g "qwerty" will be -20 points for "qwe" "wer "ert" "rty". I have got a solution to this but I was wondering if there is any way of improving it.

input = input("What is your password?")
qwerty = 'qwertyuiopasdfghjklzxcvbnm'
lower = input.lower()
for idx in range(0, len(lower) - 2):
    test_seq = lower[idx:idx + 3]
    if test_seq in qwerty:
        points -= 5
print(points)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I have rollbacked the last edit as removing code from the question makes it off-topic for Code Review. \$\endgroup\$ Oct 7 '17 at 21:46
3
\$\begingroup\$

According to my interpretation, your solution does not actually meet the specification:

If it has 3 letters adjacent/consecutive on the QWERTY UK keyboard then it will be -5 points

For example, pas does not have three adjacent keys on a QWERTY keyboard, yet it gets penalized by your code. On the other hand, your code fails to detect backwards adjacent sequences such as ewq.

\$\endgroup\$
2
  • \$\begingroup\$ If you don't mind, could you please share any ideas of how to doing this. Thank You \$\endgroup\$ Oct 8 '17 at 15:22
  • \$\begingroup\$ @olivierclotherp: Just a quick stab at fixing the problem with minimal effort for code change: change your single 'qwertyuiopasdfghjklzxcvbnm' string into a list of three strings ('qwertyuiop' , 'asdfghjkl' , 'zxcvbnm') so that you don't get false positives such as pas. \$\endgroup\$
    – Flater
    Nov 3 '17 at 10:53
1
\$\begingroup\$

I am sure your code works, but you missed initializing points to zero in what you shared here.

You can write these 2 lines:

input = input("What is your password?")
lower = input.lower()

into a single one:

lowercase_password = input('What is your password?').lower()

Your current program accepts short passwords, including empty strings which makes your passwords vulnerable to to attacks such as password guessing and brute force. To remedy to that, you can improve your code with:

password_length = 0
while(password_length < 16): # Change 16 to what is acceptable by the security standards
   lowercase_password = input('What is your password? ').lower()
   password_length = len(lower)

There are other elements to take in consideration if your goal is to assess the strength of a password.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I would also add that redefining the input function to be a string was a bad idea. \$\endgroup\$ Oct 8 '17 at 0:19
  • \$\begingroup\$ Yes, that is very true. \$\endgroup\$ Oct 8 '17 at 5:54
0
\$\begingroup\$

Taking from your example, you could do something like this:

passwd = input("What is your password? > ")
passwd = passwd.lower()
row1 = "qwertyuiop"
row1_r = row1[::-1] # this is an extended splice which steps all characters by a position of -1 which reverses the string
row2 = "asdfghjkl"
row2_r = row2[::-1]
row3 = "zxcvbnm"
row3_r = row3[::-1]
rows = [row1, row1_r, row2, row2_r, row3, row3_r] # this allows the code to check for reverse sequences 

for index in range(0, len(passwd)-2):
    sequence = passwd[index:index+3]
    for i in range(0, len(rows)-1):
        if sequence in rows[i]:
            # if you were to add 'print(sequence)' here it would show all the sequences that have consecutive characters
            points -= 5
print(points)

I have used this in my own code and it has worked perfectly.

\$\endgroup\$
5
  • \$\begingroup\$ This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review \$\endgroup\$
    – rolfl
    Feb 20 '18 at 14:08
  • 1
    \$\begingroup\$ @rolfl I see a recommendation to check for reverse sequences too, in addition to what OP did. As such, I think it passes as an answer (narrowly). Your recommendation about adding a bounty to draw attention seems out of place (auto comment huh). \$\endgroup\$
    – janos
    Feb 20 '18 at 14:35
  • 1
    \$\begingroup\$ @janos - that comment was added automatically through the review queue.... had no idea it was posted until you replied: codereview.stackexchange.com/review/low-quality-posts/92071 \$\endgroup\$
    – rolfl
    Feb 20 '18 at 16:39
  • \$\begingroup\$ @rolfl Whoa. I didn't know about this feature. I think this needs improvement (the auto-message is crap), just not sure how... \$\endgroup\$
    – janos
    Feb 20 '18 at 16:43
  • \$\begingroup\$ @rofl I probably could it improve it in someway but I do not know what way. \$\endgroup\$ Feb 22 '18 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.