Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Using Databases in Python Gettin' CRUD-y With It Add An Entry

ctrl+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
MOD
Chris Freeman
Treehouse Moderator 68,063 Points

For 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