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)]
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.
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
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?
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.
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/.
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