[Tutor] (no subject)

Glen Wheeler gew75 at hotmail.com
Tue Jun 1 23:54:46 EDT 2004


  Yes it would, but I am lazy so tried to save a little typing :).

  Glen

----- Original Message ----- 
From: Dragonfirebane at aol.com
To: gew75 at hotmail.com
Sent: Wednesday, June 02, 2004 12:35 PM
Subject: Re: [Tutor] (no subject)


Thanks.  Wouldn't it also be possible to add 'A','E','I','O','U' to vowels
so that capital vowels are caught as well, removing the need for a
case-sensitive match?

In a message dated 6/1/2004 10:18:20 PM Eastern Standard Time,
gew75 at hotmail.com writes:

  Hi Dragonfirebane,

> I'm having some trouble understanding the use of the re module in
searches, etc.
> What I am trying to do in the attached code is get the user's first and
last name
> with a maximum of 6 characters each (I don't know how to get only the
first six
> characters of each name (first, last). I'll probably divide that into one
for each
> name to make it easier) and find the number of vowels in it.  I am utterly
confused
> regarding how to do this . . . any help would be appreciated.

  Well, for such a simple task I would not use the re module.  Regular
expressions can be hard to understand, and especially for someone who is
relatively new to programming.  More intuitive is to roll your own!

  The standard string object has plenty of builtin functionality.  If you
want to only get names of maximum 6 characters, then use a while loop with
len(name) as a check.  For example:

name = raw_input("Please enter your name > ")
while len(name) > 6:
    print "Six characters maximum.  Please try again."
    name = raw_input("Please enter your name > ")

  Or if you want to get the first six characters of the input string, use a
slice.  For example:

>>> name = "Jimbiusmongius"
>>> shortname = name[:6]
>>> shortname
'Jimbiu'

  To find the number of vowels in a name, try something like :

>>> vowels = ['a', 'e', 'i', 'o', 'u']
>>> name = "Alista"
>>> for letter in name:
.  if letter.lower() in vowels:
.   vowelcount = vowelcount + 1
>>> vowelcount
3

  The reason .lower() is required is to ensure that the name contains no
uppercase letters, which are not present in our vowels list, since uppercase
and lowercase are not the same according to python.

  HTH,
  Glen



More information about the Tutor mailing list