This could be an interesting error

MRAB python at mrabarnett.plus.com
Mon Sep 1 02:23:36 CEST 2014


On 2014-09-01 01:04, Seymore4Head wrote:
> On Sun, 31 Aug 2014 16:10:27 -0600, Michael Torrie <torriem at gmail.com>
> wrote:
>
>>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!
>>
> I am still kind of shooting in the dark.
>
> I wanted to try your example and it doesn't seem to work.
> This is the latest version of changes I have made so the entire
> program looks like this:
>
> newex='Hey buddy get away from my car'
> newex = newex.split()
> sentence=""
>
> print (newex)
> wait = input ("          Wait")
>
> def pigword(test):
>      for x in range(len(test)):
>          if test[x] in "AEIOUYyaeiou":
>              stem = test [x:]
>              prefix = test [:x]
>              pigword = stem + prefix + "ay"
>              print ("Stem ",stem)
>              print ("Prefix",prefix)
>              print (pigword)
>              break
>      return (pigword)
>
> for x in range(len(newex)):
>      sentence = sentence + pigword(newex[x])+ " "
>      print (sentence)
>      wait = input ("          Wait")
>
> Trying to use your example
>>def pigword(word):
>>    for x,letter in enumerate(word):
>>        # x is index (position), letter is the value at that index
>>        if letter in "AEIOUaeiou":
> BTW I added "AEIOUYyaeiou" y as a vowel.
>
> I tried changing:
> for x in range(len(test)):
> to
> for x in enumerate(test):
>
> That causes an error to popup in a different place.  I don't
> understand why.
> Traceback (most recent call last):
>    File "C:\Functions\piglatin.py", line 26, in <module>
>      sentence = sentence + pigword(newex[x])+ " "
>    File "C:\Functions\piglatin.py", line 15, in pigword
>      if test[x] in "AEIOUYyaeiou":
> TypeError: string indices must be integers
>
Try printing out x. That should give you a clue!

> Since you included:
>>for word in list_of_words:
>>    sentence = sentence + pigword(word) + " "
> I take it you anticipated a fault here.  I don't understand why.
>




More information about the Python-list mailing list