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 trialigsm '
10,440 PointsCTRL+D handicap in cmd?
Hey. When I test my program in cmd, I cannot pass the stage when I need to hit CTRL+D as this hotkey is not perceived in the command prompt. How can I fool it?
6 Answers
Iain Simmons
Treehouse Moderator 32,305 PointsSorry, I think you also need to hit Enter after you press Ctrl+Z and it prints the ^Z. You're actually inputting an End-of-file character.
Martin Cornejo Saavedra
18,132 PointsI couldn't get it work either, but I did an alternative way to close the reader, I write quit to finish the entry.
def add_entry():
'''Add an entry'''
print("Enter your entry. Write <quit> when finished.")
final_data = ''
while(True):
data = sys.stdin.readline().strip()
if data == 'quit':
if input('Save entry? [Yn] ').lower() != 'n':
Entry.create(content=final_data)
print("Saved successfully!")
break
else:
final_data += data+'\n'
Iain Simmons
Treehouse Moderator 32,305 PointsGood idea! This method has been used by Kenneth in other Python courses, and is a bit more user-friendly in general (we shouldn't assume the user can easily hit Ctrl + D anyways, for accessibility reasons).
Alx Ki
Python Web Development Techdegree Graduate 14,822 Pointsdef add_entry():
'''Add an entry'''
print("Enter your entry. Write <quit> when finished.")
final_data = ''
while(True):
data = sys.stdin.readline().strip()
if data == '--quit':
if input('Save entry? [Yn] ').lower() != 'n':
Entry.create(content=final_data)
print("Saved successfully!")
break
else:
final_data += data+'\n'
'--quit' is an easy way to allow the word quit. (from one of Kenneth's videos).
William Cooper
3,942 PointsBy using "quit", you've made a word which can't be used in you diary entry for content: quit your job, quit a terrible program, quit working because the work day ended. If you use "QUIT" instead (loud, but effective), this is less likely to be a problem. It is also a technique used in previous example programs. Other possibilities include embedded caps, embedded special characters ("qu!t"), or nonsense strings ("mzwp") to name a few.
Iain Simmons
Treehouse Moderator 32,305 PointsIf by 'cmd' you mean you're running the Windows Command Prompt, then you'll need to use Ctrl+Z instead of Ctrl+D.
igsm '
10,440 PointsNo, it does not work. It prints out ^D or ^Z...
MUZ140920 Kudakwashe Murungu
6,316 Pointson windows its supposed to be ctrl + z, if it prints ^Z on the screen don't worry, thats the end of file character hence it will not be appended to the entry just hit enter and you are done... im using windows too.
Daniel Rosensweig
5,074 PointsIgor, you're probably way beyond this by now (I hope). I'm taking the course, apparently, almost a month later. Just in case, though:
I'm on a mac, and I had this same problem for a moment. I'm guessing you're also on a mac, even though you didn't specify, because your experience mirrors mine. As a mac user, I'm used to all sorts of commands to be labeled "control", but I actually use "command", because they're intended for windows users. When I do that, I get the same results you do, printing ^D. However, when I actually push 'control' and 'd' -- yeah, that button that you almost never use on your mac -- rather than 'command' and 'd', it seems to work.
Sometimes I have to push it twice? Not quite sure what's up with that, but ultimately it does recognize the end-of-file key sequence.
Flore W
4,744 PointsHi Daniel, did you figure out why we need to push it twice? I was wondering the same.