[Tutor] recursive function password check

Hugo Arts hugo.yoshi at gmail.com
Wed Feb 6 15:05:58 CET 2013

On Wed, Feb 6, 2013 at 1:44 PM, Mara Kelly <schooluse1992 at yahoo.com> 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")
Okay, it looks to me like you are inspecting the first and last characters
in the string, returning False if some condition is met for each, and
otherwise you chop them off and call recursively on the resulting string.
 So here's a question for you: For the first character, you check if y[0]
in vowels. But for the last character, you check if y[len(y) - 1] !=
vowels. Why did you use "in" for the first test, and "!=" for the second?

By the way, here's a related tip: in Python, negative indexes will work
from the back of the sequence. So y[-1] is the last element, y[-2] the
second to last, et cetera.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130206/dd6cf3d3/attachment.html>

More information about the Tutor mailing list