How to loop Pig Latin Translator, when incorrect input entered?


#1

Massive python noob here.

I've amended the Pig Latin Translator to include incorrect two word inputs, an incorrect empty string and a string with letters in it incorrectly. I have added raw_input('Enter a word: ') after the incorrect input conditions in the hope this would then cause a loop.

How can I loop back to the top if the condition is not met? The condition being that a single word is entered so the Pig Latin Translator can translate it.

print '\nWelcome to the Pig Latin Translator!\n'

pyg = 'ay'

original = raw_input('Enter a word: ')


if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = word[0]
    new_word = word + first + pyg
    new_word = new_word[1:len(new_word)]
    print '\n', 'In Pig Latin this is', new_word
elif len(original) > 1 and not original.isalpha() and ' ' in original:  # Includes multiple words
    print '\nJust one word please.', '\n', '\n', raw_input('Enter a word: ')
    # ADDED LOOP... BUT DOESN'T LOOP YET JUST REPEATS ONCE!!
    # ADDED LOOP... BUT DOESN'T LOOP YET JUST REPEATS ONCE!!
elif not original.isalpha() and len(original) > 0:
    print '\nThat\'s not a word.', raw_input('Enter a word: ')  # numbers were included or is just a white space
else:
    print '\nPlease enter... something.'  # input was empty
    raw_input('Enter a word: ')

#2

Have you studied any of the different looping constructs in Python, yet? If not, then set this aside and return to it once you have those concepts under your belt. If you have studied loops, let us know so we can formulate a strategy.


#3

Have we had loops yet? I don't think so, we can use a loop:

keeprunning = True
while(keeprunning):
   original = raw_input('Enter a word: ')
   if len(original) > 0 and original.isalpha():
     keeprunning = False
word = original.lower()
first = word[0]

We use a while loop, the loop will run for as long for long as the keeprunning is True, the moment if is true, the loop will be false, the user entered something valid. This is running ahead of the lesson


#4

@mtf I have but only read and not actually practised. The while statement is new to me. Can now see it is useful/necessary here.

@stetim94 I added this in and it nicely loops when incorrect input is entered. Going to work on a way of adding my print statements (why the input is incorrect) but need to understand your code first.

Thanks both


#5

While i am answering another question, what do you and what do you not understand about the code i wrote?


#6

I understand that keeprunning becomes false if the input is correct, thus ending the loop.

Think I got it - fair few things to iron out though :slight_smile:

print '\nWelcome to the Pig Latin Translator!\n'

pyg = 'ay'

keeprunning = True

while (keeprunning):
original = raw_input('\nEnter a word: ')
if len(original) > 0 and original.isalpha():
keeprunning = False
word = original.lower()
first = word[0]
new_word = word + first + pyg
new_word = new_word[1:len(new_word)]
print '\n', 'In Pig Latin this is', new_word
elif len(original) > 1 and not original.isalpha() and ' ' in original: # Includes multiple words
print '\nJust one word please.',
elif not original.isalpha() and len(original) > 0:
print '\nThat\'s not a word.', # numbers were included or is just a white space
else:
print '\nPlease enter... something.' # input was empty

EDIT i removed the last line which was raw_input('Enter a word: ') - caused the prompt to reappear if the correct input was entered instead of translating it


#7

Still recommend completing all the modules on loops before proceeding. This is not the time to explore strategies.


#8

Maybe continue with the course, so you better understand the loop? We can resume this problem after you done the loop section


#9

I agree with the moderators. so to stick to the material you learned so far (so no while statements), you can use for ... in clause, that will execute your loop fe. 5 times (below). My second idea is to make constant loop by moving your code in a function (fe. program_body() and at the last line of that function you call your function again). it will go in circles of course, but you can add an special exit like 'ESC' hit then terminate.

btw. below raw_input is changed into input and it is used only once

print ('\nWelcome to the Pig Latin Translator!\n')

pyg = 'ay'

for x in range(5):
    original = input('Enter a word: ')
    
    if len(original) > 0 and original.isalpha():
        word = original.lower()
        first = word[0]
        new_word = word + first + pyg
        new_word = new_word[1:len(new_word)]
        print ('\n', 'In Pig Latin this is', new_word)
    elif len(original) > 1 and not original.isalpha() and ' ' in original:  # Includes multiple words
        print ('\nJust one word please.', '\n', '\n')
        # ADDED LOOP... BUT DOESN'T LOOP YET JUST REPEATS ONCE!!
        # ADDED LOOP... BUT DOESN'T LOOP YET JUST REPEATS ONCE!!
    elif not original.isalpha() and len(original) > 0:
        print ('\nThat\'s not a word.')  # numbers were included or is just a white space
    else:
        print ('\nPlease enter... something.')  # input was empty

Please what am i not doing right
#10

I looked at the course, for .. in isn't taught yet. Also, this will run 5 times, even if the user inputs something valid


#11

yeah, I thought for ... in might be still ahead too. true, this is not perfect solution but a different method to approach the problem.


#12

Loops and functions are both not taught yet. I don't see how the for .. in is a better solution then a while loop, yes, it is a different solution, but why would you want to use it instead of the while loop? It is relative easy to keep the program endless running to translate as many things as you like with a additional while loop.


#13

I thought for loop was already taught and while not, so I tried to solve the problem with the knowledge at that level, but you checked for loop is still ahead. I agree while loop is better here.


#14

The following is what I pasted in my previous respons, using stetim94's while(keeprunning) loop. Run the below and it does exactly what I wanted, repeats when incorrect input is entered and finishes/translates when a single word is entered.

print '\nWelcome to the Pig Latin Translator!\n'

pyg = 'ay'

keeprunning = True

while (keeprunning):
    original = raw_input('Enter a word: ')
    if len(original) > 0 and original.isalpha():
        keeprunning = False
        word = original.lower()
        first = word[0]
        new_word = word + first + pyg
        new_word = new_word[1:len(new_word)]
        print '\n', 'In Pig Latin this is', new_word
    elif len(original) > 1 and not original.isalpha() and ' ' in original: # Includes multiple words
        print '\nJust one word please.',
    elif not original.isalpha() and len(original) > 0:
        print '\nThat\'s not a word.', # numbers were included or is just a white space
    else:
        print '\nPlease enter... something.'

#15

Read this post:

About how to make code (indent in this case) show


#17

#18

Closed to prevent fluff posts. A sure fluff magnet once it gets known. Hope you won't mind we curtail it now.

Edit: Normally I'd just delete it. And might still... It's necrotizing.