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: ')
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.
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
@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.
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
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
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
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.
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.
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.'