Reputation:
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
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.
Also ...
b
and c
to strings, the code will be easier to read, and your in
statements will still work.Upvotes: 2
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