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 trial

Python Using Databases in Python Gettin' CRUD-y With It Clean Up

igsm '
igsm '
10,440 Points

CTRL+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

Sorry, 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
Martin Cornejo Saavedra
18,132 Points

I 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'

Good 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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alx Ki
Python Web Development Techdegree Graduate 14,822 Points
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'

'--quit' is an easy way to allow the word quit. (from one of Kenneth's videos).

By 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.

If by 'cmd' you mean you're running the Windows Command Prompt, then you'll need to use Ctrl+Z instead of Ctrl+D.

igsm '
igsm '
10,440 Points

No, it does not work. It prints out ^D or ^Z...

on 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.

Igor, 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
Flore W
4,744 Points

Hi Daniel, did you figure out why we need to push it twice? I was wondering the same.