[Tutor] Python Help

Mats Wichmann mats at wichmann.us
Sat Sep 16 12:38:20 EDT 2017


On 09/15/2017 10:25 AM, Alan Gauld via Tutor wrote:
> On 15/09/17 04:08, Pratyusha Thundena wrote:
>> How do check to see if string ‘garden’ contains a 
>> vowel(a, e , i , o, u , or y) using a for loop? 
> 
> Hi, this sounds like homework and we won't do
> your homework for you, but we do give hints.
> 
> How would you do this without a computer?
> There are (at least) two ways:
> 1) you can check each letter in your word and
>    see if it is a vowel.
> 2) we can check each vowel and see if it is
>    in your word
> 
> A for loop gives you each item in a sequence one
> by one so would work in either solution.
> 
> Let's consider option 1 in pseudo-code:
> 
> for each letter in word:
>     if letter is a vowel:
>        do something... what?
> 
> So that leads to two questions:
> 1) how do you tell if a letter is a vowel?
>    Hint: look at what the string 'in' operator does
> 2) What do you want to do if you find a vowel?
>    print something? exit? or what...?
> 
> See if that helps, if you still need help come back with
> a specific question (the more specific the question, the
> more specific will be the answer) and show us your code
> plus any error messages you received.

It also leads to a chance to lecture (just what you wanted, right):

Computer programming is hard.  One of the reasons it's hard is because
you don't always know exactly what the problem you're trying to solve
consists of. Here in a learning situation it's going to be safe, but in
the real world you will often find what seemed obvious to the person who
framed the requirement may be full of ambiguities or subtle effects. Like:

Does a string contain a vowel? - you list the 5 1/2 vowels from English.

y is the 1/2: grammatically, it is considered a vowel in words like
'baby' or 'sky', but it is a consonant in words like 'yes' or 'yogurt'.
Should your solution take this into account? It would make the code
vastly more complicated, because you would have to teach it to divide
the word correctly into syllables, and apply the rule for when it is
treated as a consonant. Which means you probably need to start by
validating that the input is a correct word.  Of course you are not
going to worry about that solving a classroom problem (unless the
instructor or book is being very sneaky).

and then there is the complication of language.  should your solution
work for English only? Or for other languages? I grew up speaking a
language where åäö are vowels.  If you are trying to process information
entered by a user in a website, for example, there's a pretty reasonable
chance that it should understand other languages, and then you need to
look into character-set specifications, unicode, etc.

Enjoy learning Python!


More information about the Tutor mailing list