How can I know if my translator works properly?

After hitting run and typing in “Hello” I keep getting a time out error.

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

Can someone tell me what I am doing wrong here?
My code is:

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)]

else:
print ‘empty’

Using raw_input means the code expects a user input. This means you’d need to type something (default standard input) in the console prompt at the right hand side of the screen.

I’ve had it bug out on me before where running any code instantly threw a timeout error (as opposed to after a ceratin period of time) where I needed to either reset the lesson or entirely clear my brower cache to allow the input to work. Try less drastic steps first and hopefully you can do without and still get user input to work.

1 Like

You entered ‘latin’ twice. Why is that?

I had the same problem when using Chrome. Finally got it to work in Firefox :confused:

I was having the same issue. I fixed it by changing the second new_word variable assignment to out_word. See the code below.


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
out_word = new_word[1:len(new_word)]
print out_word
else:
print ‘empty’

My code isn’t giving me empty when I don’t type anything. It works just fine when I put words in it, but it breaks and stops working when I don’t.
Please Help

original = raw_input(‘Enter a word:’)

word = original.lower()

first = word[0]

new_word = word + first + pyg

new_word = new_word[1:]

if len(original) > 0 and original.isalpha():

print new_word

else:

print ‘empty’

THE ERROR:
Enter a word:
Traceback (most recent call last):
File “python”, line 5, in
IndexError: string index out of range

Welcome to the forums!

Try walking through your code line by line with the assumption that original = "". Why does it break instead of printing empty? If original = "" (meaning you don’t type anything for the input), using first = word[0] will cause your code to throw an error. We can’t access the first character in a string if the string is empty. How could you solve this?

1 Like

Hi

How would we make the program loop back to ask for another user input after an invalid entry?

For instance, in good old BASIC we could have used a GOTO command.

Thanks.

I would use a loop, recursion function is also possible.

Thanks, that’s my question though, how do you loop in Python?

Is there a GOTO equivalent command, or would it be a case of having to wrap the code in a function and call the function again?

Thanks!

you could use a while infinity loop for example, and then break when input is valid

Don’t think there is GOTO, and even if there was, I wouldn’t use it. Recursion with function is also possible yes, but I think the loop is easier

1 Like

Thanks.

GOTO had it’s problems, but if used properly and carefully, it was a very useful tool.

I don’t understand why this is not working for me! I am trying my best on this, but it always returns an error when I put a word. I write a word, then it loads for a few seconds, and an error shows up. It’s the same when I write nothing, and I press enter, while in this case, ‘empty’ should return.
Thanks for helping.

Are you getting an ExecTimeout? If so, that’s a quirk of CC’s environment and you should try running your code locally or using another online IDE. If not, posting your error and any relevant code would help others figure out what went wrong.

Yes! That’s the error I get, but I am not sure how to do that…

You can run your code locally by downloading an IDE (CC recommends VSCode) and installing Python, for example. It seems like this project requires Python 2, so be sure to download that version if you want your code to run properly. Or, you could try running it using another service online, like https://replit.com/.

Service online might be perfect! So, why is it giving error at codeAcademy?

I’m not sure, all I know is that it’s been a problem for a while.

Thanks for your help.

1 Like
pyg = 'ay'

print "Welcome to the Pig Latin translator!"

original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
  else:
    print "empty"
  word = original.lower()
  first = word[0]
  new_word = word + first + pyg
  new_word = new_word[1:len(original)] 
print new_word