[Tutor] recursive function password check

Dave Angel davea at davea.name
Wed Feb 6 15:53:52 CET 2013


On 02/06/2013 08:44 AM, Mara Kelly wrote:
> Hi everyone, trying to write a program that has the user enter a password, checks if it contains any vowels, and if it does prints ' It is false that password(whatever the user enters) has no vowels,' and if it has no vowels prints it is True that password has no vowels...
> Here is what I have so far...def password(y):    vowels=["a","e","i","o"]    if y[0] in vowels:        return False    if len(y) ==0:        return True    elif(y[len(y)-1] != vowels):        return False    else:        return password(y[1:len(y)-1])x=input("Enter a password:")print("It is", password(x),"that",x,"has no vowles")
> As of now it just asks for the password, and then prints 'It is False that password(whatever was entered) has no vowles' for any word I enter. I think maybe some of my if statement conditions may be being returned to the function, but then not printing the appropriate one? Can anyone help? Thanks!
>
>
>

Please don't post in html email. It can seriously mess up your columns. 
  In your case, your email program didn't even try to provide a text 
version, so those of us who read text emails can't make much sense of it.

Fortunately, I can decipher it by using someone else's reply.  I quote 
that below:

 >
def password(y):
 >     vowels=["a","e","i","o"]

You might want to include "u" in that list.  And maybe the uppercase 
versions as well.

 >     if y[0] in vowels:
 >         return False
 >     if len(y) ==0:
 >         return True
 >     elif(y[len(y)-1] != vowels):

No, you want  if y[-1] in vowels:   instead.  A string is never going to 
be equal to a list.

 >         return False
 >     else:
 >         return password(y[1:len(y)-1])
 > x=input("Enter a password:")
 > print("It is", password(x),"that",x,"has no vowles")


I presume that making the function recursive was an explicit part of the 
assignment.

-- 
DaveA


More information about the Tutor mailing list