0

I'm desperate here. Trying to do a program for one of my classes and having so much trouble with it. I added an input loop because part of the requirements is that the user has to be able to enter as many lines of code as they want. Issue is, now I get the error that the index is out of range, and I think that's because I am breaking to stop the loop.

Here's my code:

print ("This program will convert standard English to Pig Latin.")
print("Enter as many lines as you want. To translate, enter a blank submission.")
while True:
    textinput = (input("Please enter an English phrase: ")).lower()
    if textinput == "":
        break

words = textinput.split()  
first = words[0]
way = 'way'
ay = 'ay'
vowels = ('a', 'e', 'i', 'o', 'u','A', 'E', 'I', 'O', 'U')
sentence = ""

for line in text:
    for words in text.split():
        if first in vowels:
            pig_words = word[0:] + way
            sentence = sentence + pig_words
        else:
            pig_words = first[1:] + first[0] + ay
            sentence = sentence + pig_words
print (sentence)

I am definitely an amateur and could use all the help/advice I can get.

THANKS SO MUCH

Teresa
  • 1
  • 1
  • 1

4 Answers4

2

In your while loop, because you're testing for textinput == "" after you've already set textinput = input(), that means that when it breaks, textinput will always be ""! The index out of range error then arises when you try to access words[0]; there are no elements in "", so you'll get an error.

Also, since you're overwriting the value of textinput every time you go through the while loop, you can't actually keep track of all the previous things the user has entered, since textinput keeps on changing. Instead, you can put all the code under the while loop into the while loop. Try:

print("This program will convert standard English to Pig Latin.")
print("Enter as many lines as you want. To translate, enter a blank submission.")
while True:
    textinput = (input("Please enter an English phrase: ")).lower()
    if textinput == "":
        break
    words = textinput.split()  
    way = 'way'
    ay = 'ay'
    vowels = ('a', 'e', 'i', 'o', 'u','A', 'E', 'I', 'O', 'U')
    sentence = ""

    for word in words:
        for first in word:
            if first in vowels:
                pig_words = first[0:] + way
                sentence = sentence + pig_words
            else:
                pig_words = first[1:] + first[0] + ay
                sentence = sentence + pig_words
    print(sentence)

(By the way, you didn't define text either when you wrote "for line in text", and you never actually used "line" in that for loop. Just little notes to look out for, good luck!)

eyqs
  • 196
  • 4
  • 7
0

You're reassigning the textinput variable on every loop iteration. Instead, you could try something like:

textinput = ""
while True:
    current_input = (input("Please enter an English phrase: ")).lower()
    if current_input == "":
        break
    else:
        textinput += current_input
komaromy
  • 221
  • 2
  • 12
0

Your problem exist because the break statement only break out of the while loop, and then it will continue running words = textinput.split() and onwards.

To stop the script when you received a empty input, use quit() instead of break.

print ("This program will convert standard English to Pig Latin.")
print("Enter as many lines as you want. To translate, enter a blank submission.")
while True:
    textinput = (input("Please enter an English phrase: ")).lower()
    if textinput == "":
        quit()
John Jam
  • 185
  • 1
  • 1
  • 17
0

You can keep reading data and processing it separate by using 2-argument form of iter:

from functools import partial

for line in iter(partial(input, "Eng pharse> "), ""):
    print(line) # instead of printing, process the line here

It's simpler than it looks: when you giveiter 2 arguments and iterate over what it returns, it'll call the first argument and yield what it returned, until it returns something equal to second argument.

And partial(f, arg) does the same as lambda: f(arg).

So the code here above prints what he read until user enters empty line.

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52