I've tried finding the solution to this problem using Google as well as spending hours fooling around to try to fix the problem.
I'm a bit new at programming and decided to take Python as a first language.

I know I could easily get around this by doing it a different way, but I wanted to figure out why this way wasn't working.

Here is the code:

#---START---
##SETUP
import string
from string import *
#--------------------------------------------------------------------------
def pigTranslator(text):
    list = text.split()
    list = puncSep(list)
    wordsWithCaps = capRemem(list)
    list = makeLC(list)
    list = pigTranslate(list)
    list = capFromMem(list, wordsWithCaps)

    list = addSpaces(list)
    return join(list,"")
#--------------------------------------------------------------------------
def pigLatin(word):
    if word[0] in "aeiouAEIOU":
        return word + "way"
    else:
        count = 1
        for char in word[1:]:
            if char in "aeiouAEIOUyY" and not (word[count] == "u" and word[count - 1] == "q"):
                return word[count:] + word[:count] + "ay"
            else:
                count = count + 1
        return word + "ay"
#--------------------------------------------------------------------------
##Seperates words with punctuation into 2 seperate words.
def puncSep(list):
    currentWord = -1
    for word in list:
        currentWord = currentWord + 1
        if word[-1] in punctuation:

#            list = list[:currentWord] + [word[0:-1], word[-1]] + list[currentWord + 1:]
            list[currentWord:currentWord + 1] = [word[:-1], word[-1]]
            currentWord = currentWord + 1
    return list
#--------------------------------------------------------------------------
##Creates a list stating the word# of each word beginning with a capital.
def capRemem(list):
    currentWord = -1
    wordsWithCaps = [[], []]
    for word in list:
        currentWord = currentWord + 1
        if word != upper(word):
            if word[0] in uppercase:
                wordsWithCaps[1] = wordsWithCaps[1] + [currentWord]
        else:
            wordsWithCaps[0] = wordsWithCaps[0] + [currentWord]
    return wordsWithCaps
#--------------------------------------------------------------------------
##Makes all words in list completely lowercase
def makeLC(list):
    currentWord = -1
    for word in list:
        currentWord = currentWord + 1
        list[currentWord] = lower(word)
    return list
#--------------------------------------------------------------------------
def pigTranslate(list):
    currentWord = -1
    for word in list:
        currentWord = currentWord + 1
        if word not in punctuation:
            list = list[:currentWord] + [pigLatin(word)] + list[currentWord + 1:]
    return list
#--------------------------------------------------------------------------
def capFromMem(list, wordsWithCaps):
    for reference in wordsWithCaps[1]:
        list[reference] = capitalize(list[reference])
    for reference in wordsWithCaps[0]:
        list[reference] = upper(list[reference])
    return list
#--------------------------------------------------------------------------
def addSpaces(list):
    lastWord = -1
    for word in list[1:]:
        lastWord = lastWord + 1
        if word not in punctuation:
            list[lastWord] = list[lastWord] + " "
    return list
#--------------------------------------------------------------------------
##Function Calls
w = raw_input("enter a sentence: ")
print pigTranslator(w)
print ""
#---END---

Here is the code I am having trouble with:

##Seperates words with punctuation into 2 seperate words.
def puncSep(list):
    currentWord = -1
    for word in list:
        currentWord = currentWord + 1
        if word[-1] in punctuation:
#            list = list[:currentWord] + [word[0:-1], word[-1]] + list[currentWord + 1:]
            list[currentWord:currentWord + 1] = [word[:-1], word[-1]]
            currentWord = currentWord + 1
    return list


The line that is commented out is the way I had been doing it (which worked fine).  I just wanted to try doing it this way.
However, when this runs (use "U?" as an example) it returns an error ("IndexError: string index out of range").

I was wondering if any of you have any idea why this might be happening and how I might get around this without going about it a different way.
Also, if anyone could be so kind as to explain the difference in effect between the first method and the second method.


Thank in advance to anyone who replies with some help.


~Matt
_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig

Reply via email to