This could be an interesting error

Seymore4Head Seymore4Head at Hotmail.invalid
Mon Sep 1 00:07:50 CEST 2014


On Sun, 31 Aug 2014 22:53:01 +0100, MRAB <python at mrabarnett.plus.com>
wrote:

>On 2014-08-31 22:02, Seymore4Head wrote:
>> import math
>> import random
>> import sys
>>
>> ex='Hey buddy get away from the car'
>> newex = ex.split()
>> sentence=""
>>
>> print (newex)
>> wait = input ("          Wait")
>>
>> 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)
>>
>> for x in range(len(newex)):
>>      sentence = sentence + pigword(newex[x])+ " "
>>      print (sentence)
>>      wait = input ("          Wait")
>>
>> The program seems to work and it does work with everything I have
>> tried so far.  The one exception is if you change "the" to "my" (in
>> the first line) the program crashes.
>>
>>
>> Traceback (most recent call last):
>>    File "C:\Documents and
>> Settings\Administrator\Desktop\Functions\test.py", line 25, in
>> <module>
>>      sentence = sentence + pigword(newex[x])+ " "
>>    File "C:\Documents and
>> Settings\Administrator\Desktop\Functions\test.py", line 22, in pigword
>>      return (pigword)
>> UnboundLocalError: local variable 'pigword' referenced before
>> assignment
>>>>>
>>
>'my' doesn't contain a vowel, therefore the condition of the 'if'
>statement in 'pigword' is never true, therefore it never binds to the
>name 'pigword'.
>
Ah.   The piglatin example says to use y as a vowel.  I forgot to
include it.
Thanks
>BTW, you have a function called 'pigword' that has a local name also
>called 'pigword'. Try not to do that, because you could easily confuse
>yourself.

I am a pro at confusing myself.  :)



More information about the Python-list mailing list