user7792600
user7792600

Reputation:

Password Maker Python Lists

This program is supposed to prompt the user to make a password, however only 
certain characters are allowed to be used. 
def password(z):
    n=0
    w=0
    a=0
    b=['!','@','#','$','%','^','&','*','?']
    c=['.',',',':',';','[',']','{','}','(',')','<','>','|','~','-
       ','=','+','_']
    for ch in z:
        if any(ch in c for ch in z):
            z=raw_input("Illegal characters. Please try again: ")
            return password(z)
        else:
            if any(ch.isdigit() for ch in z):
                n+=1
            if any(ch in b for ch in z):
                w+=1
            if any(ch.isalpha() for ch in z):
                a+=1
            if n==0 or w==0 or a==0:
                m=raw_input("Please use at least one letter, number, and special character: \n")
                return password(m)
            if n>0 and w>0 and a>0:
                y=raw_input("Please retype password: ")
            if y==z:
                print "Password confirmed"
                break
            else:
                y=raw_input("Not the same password. Please create a new password: ")
                return password(y)

z=raw_input('Enter a password: ')
print password(z)

UPDATE: This now works. I'm however a bit confused as to why it does, since I don't know why I needed to put the any method for ch in b, cause I thought that if ch in b means that if any ch is in the list b. Regardless, it now works.

Upvotes: 0

Views: 136

Answers (2)

Prune
Prune

Reputation: 77837

The function returns None, because you have no return statements at all. None is the default return value. Your logic is somewhat convoluted, especially the superfluous recursion, but I think you can fix this by simply adding

return z

to the bottom of the function.


FOLLOW-UP

I revisited this ... and your logic appears to have a variety of problems.

  • Your indentation is faulty near the bottom of the loop; this code won't reach execution.
  • int() always returns 0. I think you want ch.isdigit() here
  • You check each character for being 0, then in the "b" list, then in the "c" list ... and as soon as you find all three, you move on to confirming the password. Is that the logic flow you wanted?

Also ...

  • Single-letter variable names have very little information. Use descriptive ones.
  • For simple exists / doesn't exist variables, use Boolean, not 0 and 1. Again, this will make your code easier to read.
  • If you change b and c to strings, the code will be easier to read, and your in statements will still work.

Upvotes: 2

C. Brooks
C. Brooks

Reputation: 48

The reason your function returns None, is because your function doesn't return anything when it is successful.

Also, even if you did add a return statement, your check for integers is not correct. one correct way to check this would be ch.isdigit()

Upvotes: 0

Related Questions