FAQ: Learn Python - Pyglatin - Testing, Testing, is This Thing On?

If you’re learning anything at all, it is that there are workarounds. Put that learning to use.

Trying to add into the translator. I have done the below so far to allow the ability to escape if someone types in wrong input. What I would like to happen is if the input is wrong to be prompted again for a word and if wrong 3 times then ask if they want to quit. I was thinking a loop but I started with the below and it feels clunky and I want to implement this correctly. I know that a function could do this but memory limits etc… Thanks in advance for the help.

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 new_word
  print 'No Numbers or Spaces.. only letters'
while True:
    cmd = raw_input('Do you want to quit? Enter \'q\'!')
    if cmd == 'q':

I keep getting: Traceback (most recent call last):
File “python”, line 3, in
ExecTimeoutException: Program took too long to terminate.

After you click Run, be sure to click the prompt in the console. Then type a word and Enter. If that doesn’t work, then comment out the input statement and assign a literal value so you can run the program to a final result. That will get you through the exercise.

This is my code:
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 new_word


print 'empty'

When I type anything, why does it return ‘empty’?

Comment out that line and just hard code a word.

original = "surprise"

Then you can see the code run and pass the lesson.

I had to put show solution to every exercise because it wouldn’t let me move forward. Where do I type the word to enter into the translator?

worked very well for me!!! THANKS!! do you know why theres a difference between the space between : and 'tho?

It’s simply for visual effect. Without it, there wouldn’t be a space between the colon and the user’s input in the console.

Without Space

Enter a word:hello

With Space

Enter a word: hello

Thanks for the reply. However, I think there’s more than that as there’s a difference in the output.

When I type

original = raw_input (‘Enter a word:’)
original = raw_input ('Enter a word : ')

the first one prints empty no matter what’s my input.

What do you mean by “prints empty”?

print ("Empty")

according to the instructions, as I recall.

1 Like

my bad, I tried to say it prints nothing at all for the first input. my code now, but my question was similar to sliosi, and was solved by the space issue suggested by design7184503999 - wanted to know why did the space change the output… it somehow works now; sorry if i caused any confusion :sweat_smile:

1 Like

raw_input seems not working for this lesson.
I am using the solution or replacing raw_input with string to proceed.

1 Like

I need help, my code is the same as the solution, but it keeps saying that there is an error on line three?

Suggest read through this topic and note some of the suggestions and workarounds.

Hello my code isn’t working it isn’t even repeating the word

Hey! I hope you all are doing allright!

I’ll start by saying that I started this course few months ago, but I didn’t complete it, so I’m doing all the exercises again. I felt this exercise had many redundancies, but I get it, since the point of it is to let people use everything they’ve learned up to this point. Anyways, at the very last exercise I changed some things just to make the code a bit more straightforward and a bit less boring. In this process, I used a few bits of code that have not been studied up to this point in the course, and even though it is good (I think), I still think it could be better.

I want to share my code with the community, with it’s improvements and it’s flaws. If you are more experienced, I hope you can give me some feedback on how to make better code, and if you are a begginer, please learn from what I did right or wrong, according to what the more experienced people say.

print('Pig Latin translator') #This is just to print a nice title before the input. print('') pyg = 'ay' new_word = '' #I declared the new_word variable earlier in the code as an emptry string so I could use it to make a while loop. #The new_world variable will remain empty until the user gives an imput containing only latin letters. while not new_word: original = input('Enter a word: ') if original.isalpha(): original = original.lower() new_word = original[1].upper() + original[2:len(original)] + original[0] + pyg #Now the variable is not empty, and the loop will end. print('Your word translated to Pig Latin is: %s' % new_word) elif original: print('You entered %s, but it contains non alphabetical characters. Please, try again.' % original) #The loop will restart. else: print('You entered nothing, it seems. Why would you do that? Please, try again.') #The loop will restart

Happy coding! :slight_smile:

Hi all,
I’m having trouble understanding why len is used here:

new_word = new_word[1:len(new_word)]

If you just wanted 1: of new_word, what is the purpose of len here? Thanks!

It’s just there to show explicitly what the value will be if that parameter is left off.