Python Case Sensitive Help....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samanthavernon
    New Member
    • Feb 2008
    • 1

    Python Case Sensitive Help....

    Hi All,
    I'm using Windows Vista with Python 2.5. I need the code to check for case sensitive letters and digits, but something isn't right with it.

    The code is below:

    Code:
    # -*- coding: cp1252 -*-
    from easygui import *
    
    accounttype=buttonbox("Would you like to create an account or login?", "Password Storage System Pro", choices=["Login","New Account"])
    
    if accounttype=="Login":
        multpasswordbox("Enter Your Account Information", "Login", ["User ID","Password"])
        msgbox("Thank you for using Password Storage System Pro, \nPlease Try Again Later.")
    if accounttype=="New Account":
        newaccount=multpasswordbox("Enter Your Account Information", "New Account", ["User ID","Password"])
        confirm=passwordbox("Please Validate Your Password", "Password")
        while newaccount[1]==confirm:
            msgbox("Thank you for using Password Storage lolSystem Pro")
            password=confirm[1]
            i=0
            cap=0
            low=0
            dig=0
            while i<len(password):
                if password[i] >= "A" and password[i] <= "Z":
                    cap=cap+1
                if password[i] >= "a" and password[i] <= "z":
                    low=low+1
                if password[i] >= "0" and password[i] <= "9":
                    dig=dig+1
                i=i-1
                if cap>0 and low>0 and dig>0:
                        print "Thank You for registering"
    
    print cap
    print low
    print dig
    
    if newaccount[1]!=confirm:
        msgbox("Your Passwords did not match. Try again please.")
        confirm=passwordbox("Please Validate Your Password", "Password")
    password=confirm[1]
    i=0
    cap=0
    low=0
    dig=0
    while i<len(password):
        if password[i] >= "A" and password[i] <= "Z":
            cap=cap+1
        if password[i] >= "a" and password[i] <= "z":
            low=low+1
        if password[i] >= "0" and password[i] <= "9":
            dig=dig+1
    i=i-1
                    
    
    print cap
    print low
    print dig
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Following are a some different approaches that may help you.
    [code=Python]import re

    pattU = re.compile(r'[A-Z]')
    pattL = re.compile(r'[a-z]')
    pattD = re.compile(r'[0-9]')

    word = 'AdjiV560dfRGh'

    print pattU.findall(w ord)
    print pattL.findall(w ord)
    print pattD.findall(w ord)

    print len(pattU.finda ll(word))

    >>> ['A', 'V', 'R', 'G']
    ['d', 'j', 'i', 'd', 'f', 'h']
    ['5', '6', '0']
    4[/code][code=Python]import string

    word = 'AdjiV560dfRGh'
    print len([letter for letter in word if letter in string.ascii_lo wercase])
    print len([letter for letter in word if letter in string.ascii_up percase])
    print len([letter for letter in word if letter in string.digits])

    >>> 6
    4
    3
    >>>[/code]
    Instead of duplicating inline code, you can use a function[code=Python]# Function returns True if word contains upper case, lower case and digits
    # Otherwise returns false
    def validate_word(w ord):
    tt = (len([letter for letter in word if letter in string.ascii_lo wercase]), \
    len([letter for letter in word if letter in string.ascii_up percase]), \
    len([letter for letter in word if letter in string.digits]))
    if 0 in tt: return False
    return True

    print validate_word(' guenhfnm58976')
    print validate_word(' guenhfASZnm5897 6')

    >>> False
    True
    >>> if validate_word(' guenhfASZnm5897 6'):
    ... print 'Success'
    ... else:
    ... print 'Failure'
    ...
    Success
    >>> [/code]

    Comment

    Working...