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,130 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,810 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,727 Points

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