This could be an interesting error

Michael Torrie torriem at gmail.com
Mon Sep 1 00:10:27 CEST 2014


On 08/31/2014 03:02 PM, Seymore4Head wrote:
> def pigword(test):
>     for x in range(len(test)):
>         if test[x] in "AEIOUaeiou":
>             stem = test [x:]
>             prefix = test [:x]
>             pigword = stem + prefix + "ay"
>             print ("Stem ",stem)
>             print ("Prefix",prefix)
>             print (pigword)
>             break
>     return (pigword)

So, what do you think will happen if the word contains no vowels?  Where
is pigword defined?

> for x in range(len(newex)):
>     sentence = sentence + pigword(newex[x])+ " "
>     print (sentence)
>     wait = input ("          Wait")

You don't need to iterate over range(len(blah)).  The standard idiom
when you need index as well as the item itself is to iterate over
enumerate().  Or if you don't need the index, just iterate directly.
You can iterate directly over the list, or the letters in the word,
optionally getting an index. It's much cleaner and less error prone.
Consider something like:

def pigword(word):
    for x,letter in enumerate(word):
        # x is index (position), letter is the value at that index
        if letter in "AEIOUaeiou":
            ...

for word in list_of_words:
    sentence = sentence + pigword(word) + " "
    ...

That doesn't solve your little logic problem, though I think you can
figure that part out easily!





More information about the Python-list mailing list