Python Forum
Password checking - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Password checking (/thread-448.html)



Password checking - nzieno - Oct-11-2016

I am making a program to check if a password is atleast 8 characters long, has atleast 1 special character, has atleast 1 number and atleast 1 capital letter. 

   password = eval(input("Please enter a password:",))

def check(password):
     """Returns True only if password is strong enough."""
     if  (len(password) >= 8
      and any(char.isdigit() for char in password)
      and any(char.islower() for char in password) 
      and any(char.isupper() for char in password)):
      print("This is an acceptable password")         
      return True
     else:
         print("This is not a acceptable password")
         return False
        
check()

  
If someone could show a correct way of doing this that was would be awesome. And without getting to technical I'm still just getting into python. Ive only just gotten into functions.


RE: Password checking - wavic - Oct-11-2016

Wow! Where did you get this code from.
Never, NEVER evaluate the user input. Eval() interprets a string as a code. What you are doing is to get the user input and run it. If the input holds a python code it will be executed. Hacking in the pure meaning.

import string

if len(password) >= 8 and string.punctuation in password and \
        string.ascii_uppercase in password and string.digits in password:
    #correct password!
Ups! Wrong code. Do not use it  :rolleyes:


RE: Password checking - Mekire - Oct-12-2016

This really seems like you copied some of my code from another thread (or copied code from someone who did) without really understanding it.  You never passed the input as an argument to the function.

What is your exact problem here?  What is the error?


RE: Password checking - wavic - Oct-12-2016

re?

I can't do it. Regular expressions are my weakness.


RE: Password checking - Mekire - Oct-12-2016

Quote:re?

I can't do it. Regular expressions are my weakness.


Neah, let's get sadistic:
def check(password):
    test = (str.isdigit, str.isupper, str.islower)
    good_enough = all(any(f(p) for p in password) for f in test)
    return len(password) >= 8 and good_enough
Note: if you try to turn the above into a professor and you don't know basic python they will get very suspicious.


RE: Password checking - Skaperen - Oct-15-2016

(Oct-12-2016, 03:49 AM)Mekire Wrote: Note: if you try to turn the above into a professor and you don't know basic python they will get very suspicious.

we will end up with too many professors.


RE: Password checking - nilamo - Oct-17-2016

(Oct-12-2016, 03:49 AM)Mekire Wrote: Neah, let's get sadistic:
def check(password):
    test = (str.isdigit, str.isupper, str.islower)
    good_enough = all(any(f(p) for p in password) for f in test)
    return len(password) >= 8 and good_enough
Note: if you try to turn the above into a professor and you don't know basic python they will get very suspicious.

Why not make the length one of the tests?
def check(password):
    test = (str.isdigit, str.isupper, str.islower, lambda x: len(x)>=8)
    return all(any(f(p) for p in password) for f in test)



RE: Password checking - micseydel - Oct-18-2016

Not quite @[nilamo], the length requirement you have is being applied to individual characters in the string.