Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Password checking
#1
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.
Reply
#2
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:
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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?
Reply
#4
re?

I can't do it. Regular expressions are my weakness.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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.
Reply
#6
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
(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)
Reply
#8
Not quite @[nilamo], the length requirement you have is being applied to individual characters in the string.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020