Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialshibashis
8,989 Pointsctrl+d throws EOFError on next line containing input prompt
I am trying to do the exercise in python 2.7 and getting EoF error on pressing control+d. Is there a way to get it working in pyton 2.7?
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsFor Python 2.7, input()
is expecting an object such as a literal number or a quote delimited string. When a Cntl-D is input it is interpreted as a End of File marker. For this reason many of the exercises suggest using a key input such as "DONE" to indicate completion. If you wish to use Cntl-D you will need to wrap your input()
in a try/except
statement. Below is an example run in ipython with Python 2.7:
# First example fails since Python 2.7 expects delimited strings
In [7]: try:
...: resp1 = input("> ")
...: print("input is " + resp1)
...: except:
...: print("Error caught")
...:
> Works!
Error caught
# Using quoted string this time
In [8]: try:
resp1 = input("> ")
print("input is " + resp1)
except:
print("Error caught")
...:
> 'Works!'
input is Works!
# Entering Cntl-D at prompt
In [9]: try:
resp1 = input("> ")
print("input is " + resp1)
except:
print("Error caught")
...:
> Error caught