-1

I am doing some work for my computing class at school and we need to ask for input of a password. The input needs to be between 6 and 12 characters, and contain uppercase, lowercase and numbers in it. I have this so far :

import sys
import os
def checkPass():
    passLoop = True
    while passLoop:
        print("Welcome user!")
        x = len(input("Please enter your password, between 6 and 12 characters. "))
        if x < 6:
            print("Your password is too short")
            r = input("Please press any key to restart the program")
        elif x > 12:
            print("Your password is too long")
            r = input("Please press any key to restart the program")
        else:
            print("Thank you for entering your password.")
            print("Your password is strong. Thank you for entering!")
            passLoop = False

checkPass()

I need your help checking for uppercase, lowercase and integers. I'm only young so please don't be too harsh!

user3411623
  • 29
  • 1
  • 6
  • Hint: store the password itself in a variable somewhere. Right now you're only storing the length. – Kevin Oct 15 '14 at 15:47
  • possible duplicate of [Checking the strength of a password (how to check conditions)](http://stackoverflow.com/questions/16709638/checking-the-strength-of-a-password-how-to-check-conditions) – Paul Oct 15 '14 at 15:49
  • Unfortunately for you this is not how stackoverflow works. We can help you with some specific problem you are having, but we won't finish your homework for you. – Benjamin Bannier Oct 15 '14 at 15:50
  • So if I done x = password ? – user3411623 Oct 15 '14 at 15:50
  • Sorry benjamin, this isn't what I was asking though! I was just asking for hints on checking strength such as what kevin said! – user3411623 Oct 15 '14 at 15:51
  • Funny how the duplicate above also restricts 6 to 12 chars. Perhaps both professors used the same textbook? – Paul Oct 15 '14 at 15:52
  • @user3411623: To be honest, I didn't read that in your question. – Benjamin Bannier Oct 15 '14 at 16:23

3 Answers3

2
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d).{6,12}$

You can try this.This employs re.

You can use it like

import re
if re.match(r"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d).{6,12}$",pass,re.M):
        print "valid"
else:
        print "invalid"

See demo.

http://regex101.com/r/dZ1vT6/28

vks
  • 67,027
  • 10
  • 91
  • 124
0

the any function in combination with a map can be helpful. The map function iterates over all characters of the given string and test via the given lambda function if the character is an uppercase. The is consumed by the any function which returns true on the first true from the map function

 >>> any(map(lambda a:a.isupper(),"aba"))
 False
 >>> any(map(lambda a:a.isupper(),"aBa"))
 True

you can wrap that into a separate function like

>>> def test(passwd,func):
        return any(map(func,passwd))

>>> test("aBa",str.isupper)
    True
>>> test("ab0",str.isdigit)
    True
>>> test("aBa",str.islower)
    True
greole
  • 4,523
  • 5
  • 29
  • 49
0

let's assume you have the password stored in a variable named passwd. Then we can take your literal requirements and write checks for them:

The input needs to be between 6 and 12 characters,

if not 6 <= len(passwd) <= 12: ...

and contain uppercase,

if not any(c.isupper() for c in passwd): ...

lowercase

if not any(c.islower() for c in passwd): ...

and numbers in it.

if not any(c.isdigit() for c in passwd): ...

On a side note: you really should not limit the length of passwords.

ch3ka
  • 11,792
  • 4
  • 31
  • 28