I created a program awhile back to translate a phrase into pig latin. Pretty simple program, but now I am trying to figure out how to get it to read from a file and process it that way. Here is my old program;

def main():
    enter = raw_input("Enter a phrase to translate: ")
    print enter, "==>", change(enter)

def vowel(text):
    for i in range(len(text)):
        if test(text[i]) == True:
            return i

def trans(word):
    #Translates the individual words
    x = vowel(word)
    if x == None:
        output = (word + "yay")
    elif x == 0:
        output = (word + "yay")
    elif x >= 1:
        output = (word[x:] + word[:x] + "ay")
    else:
        pass
    return output

def change(phrase):
    #allows the program to translate the entire phrase
    y = phrase.split()
    t = str()
    for word in range(len(y)):
        t = (t + trans(y[word]) + " ")
    return t

def test(char):
    #Will test if the first letter is a vowel
    vowels="AaEeIiOoUu"
    for check in vowels:
        if char == check:
            return True
    return False

main()

I need to take that and get it to read from a txt file that contains the following:


roses are red
violets are blue

etc.

Seems like a pretty simple solution I just can't seem to figure out how to get it to work.

Recommended Answers

All 8 Replies

"Can't get it to work" is not helpfull comment, post the Error messages verbatim. Also help how did you tried to debug the program based on the error message. Debugging is essential skill in programming and we do not want to make you not to learn it.

"Can't get it to work" is not helpfull comment, post the Error messages verbatim. Also help how did you tried to debug the program based on the error message. Debugging is essential skill in programming and we do not want to make you not to learn it.

What I am having trouble with is where to place the open(".txt", 'rU'). All I really want to know is if the basic input statement could be easily replaced by that or if I am going to have to make up a new function. I did a lot of troubleshooting and have come up short every time. Lots of errors saying there is nothing inside the file (which the certainly is.) I feel like I am just missing some little thing. I have spent what I feel is too much time trying to modify the program I already created to read from a file. I tried the "open" operation inside the 'def main:' made a new one called 'def initialize():' also tried putting it outside of all of the functions.

some example errors I am experiencing

File "C:\User\piglatin.py", line 28, in change
for word in range(len(y)):
TypeError: object of type 'file' has no len()

I tried this

fObj = open("English.txt", 'rU')

def vowel(text):
    for i in range(len(text)):
        if test(text[i]) == True:
            return i

....

def main():
    a = change(fObj)
    var1 = []
    for text in a:
        s=text.split(" ")
        var1.append(a)
    print a, "==>", change(fObj)

That is just one of the many way I have been playing with it.

You would substitute a record from a file in place of the raw_input and send it to the function named "change". Opening and reading files.

You would substitute a record from a file in place of the raw_input and send it to the function named "change". Opening and reading files.

Thanks. I think I am on the right track. I'm not getting errors, but when I print it it returns:

<open file 'piglatin.txt', mode 'rU' at 0x0000000002A2F4B0> ==> <function change at 0x0000000002A7C9E8>

I'll keep working at it

Maybe you should use the result somewhere?

s=text.split(" ")

I have been throwing around code and not really getting anywhere. I only end up back to just changing the main to:

def main():
    enter = open("English.txt", 'rU')
    print enter, "==>", change

As for the split, I don't know where to put it. Thanks for all of the help anyways.

Here is my current problem:

enter = open("piglatin.txt", 'rU')
def main():
    for word in enter:
        s=word.split(" ")
        st = s[0].strip() + " " + s[1].strip() + " " + s[2].strip()
        print st
        print "==>"
        print change
    
def vowel(text):
    for i in range(len(s)):
        if test(s[i]) == True:
            return i

def trans(word):
    #Translates the individual words
    x = vowel(word)
    if x == None:
        output = (word + "yay")
    elif x == 0:
        output = (word + "yay")
    elif x >= 1:
        output = (word[x:] + word[:x] + "ay")
    else:
        pass
    return output

def change(word):
    #allows the program to translate the entire phrase
    y = word.split(" ")
    t = str()
    for word in range(len(y)):
        t = (t + trans(y[word]) + " ")
    return t

def test(char):
    #Will test if the first letter is a vowel
    vowels="AaEeIiOoUu"
    for check in vowels:
        if char == check:
            return True
    return False



main()

And it prints:

roses are red
==>
<function change at 0x0000000002B8C9E8>
violets are blue
==>
<function change at 0x0000000002B8C9E8>
sugar is sweet
==>
<function change at 0x0000000002B8C9E8>
but not as
==>
<function change at 0x0000000002B8C9E8>

Traceback (most recent call last):
File "C:\Users\Hegwood\Desktop\PA2_60.py", line 48, in <module>
main()
File "C:\Users\Hegwood\Desktop\PA2_60.py", line 7, in main
st = s[0].strip() + " " + s[1].strip() + " " + s[2].strip()
IndexError: list index out of range

I know that just adding the index together wont work for more that 3 words but not sure how to fix that? Also The function "change", I did something similar to the top but it didnt turn out like I'd hoped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.