[Tutor] recursive function password check

Dave Angel davea at davea.name
Wed Feb 6 15:46:39 CET 2013


On 02/06/2013 09:26 AM, Noriko Tani wrote:
> Hi Mara,
>
> Several suggestions:
>
> Put the password in a list, then loop each letter to check if it is a vowel like this

No need to make it a list, strings are already iterable.  And you don't 
make it a list in the code, just in the remark above.

>
> password=[]
> vowels=['a','e','i','o','u']            #in your message, u is missing, BTW
> password=input("Enter a password:")
>
> for p in password:

This loop goes around exactly once, since you return no matter what.

>      if p in vowels:
>          return True          #I would use print("the message that you want to display") though.

Shouldn't mix "calculation" and "output" unless it's just for play.

>      else:
>          return False

This return should be dedented to line up with the for statement, so it 
only executes if none of the earlier characters was a vowel.
>
> And how about error handling?  You put
>      if len(y) ==0:
>          return True          #I would use print("woops! You did not enter anything.") and add break

But in the OP's code, this return is triggered on any string with no 
vowels.  Note, the function is recursive, and this is the only final 
exit condition.

>
> Does this password case sensitive?  If so, add AEIOU in vowels list.
>
> Good luck!
>
> Noriko
>
> ________________________________
> From: Tutor [mailto:tutor-bounces+noriko.tani=tomtom.com at python.org] On Behalf Of Simon Yan
> Sent: Wednesday, February 06, 2013 9:11 AM
> To: Mara Kelly
> Cc: tutor at python.org
> Subject: Re: [Tutor] recursive function password check
>
>
>
> On Wed, Feb 6, 2013 at 9:44 PM, Mara Kelly <schooluse1992 at yahoo.com<mailto: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")
>
> 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!
>
> It appears that the issue is from this:
>
> elif(y[len(y)-1] != vowels):
>
> This condition will be true because you are comparing a string with a list. Thus causing passwrod() returning False.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org<mailto:Tutor at python.org>
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
> --
> Regards,
> YeeYaa (Simon Yan)
>
> http://simonyan.fedorapeople.org/
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
DaveA


More information about the Tutor mailing list