[Tutor] recursive function password check
ramit.prasad at jpmorgan.com
Wed Feb 6 17:31:12 CET 2013
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):
> if y in vowels:
> return False
> if len(y) ==0:
> return True
> elif(y[len(y)-1] != vowels):
> return False
> 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!
Why look at each character in the password? Unless you have a very short
password it will be more inefficient than looking for the vowels. Also,
if you lowercase the incoming password then you can avoid looking
for uppercase vowels. Note: if you have to deal with international
character sets (code pages) then you might not want to lower. If you
did not understand the previous statement, then chances are it does
not apply to you. :) I will leave the following code snippet with you
to adapt into your program.
y = y.lower()
if vowel in y:
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
More information about the Tutor