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 Add An Entry

Luiz Cesar Merjan de Paula
Luiz Cesar Merjan de Paula
4,096 Points

Why this code doesn't work with PyCharm?

When i tried to run this code with PyCharm it's wasn't word and showed this error mensage: EOFError: EOF when reading a line

But when i tried with Python IDLE, its work perfectly.

Why this code wasn't work why PyCharm?

3 Answers

Bogdan Lalu
Bogdan Lalu
6,419 Points

^+D does not work with PyCharm it seems, I am having the same problem now. It does not work in the command prompt either, when running "python diary.py". I've discovered however that CTRL+Z works in the command prompt. I'm using PyCharm on Windows 10

Vitaly Myshlaev
Vitaly Myshlaev
5,883 Points

I also had the same EOF error. I'm using PyCharm (Community Edition for Mac) for learning:

Traceback (most recent call last): Save entry? [Y]/n File "/Users/Documents/Code/Learning Python/dairy.py", line 67, in <module> menu_loop() File "/Users/Documents/Code/Learning Python/dairy.py", line 37, in menu_loop menuchoice File "/Users/Documents/Code/Learning Python/dairy.py", line 46, in add_entry if input('Save entry? [Y]/n ').lower() != 'n': EOFError: EOF when reading a line Process finished with exit code 1

So I've just changed the add_entry() to get multiple line entry, as stdin is the topic of the course:

def add_entry():
    """Add an entry."""
    entry = ""
    print("Log you idea, print 'done' to finish.")
    while True:
        data = input('> ')
        if data.lower() != 'done':
            entry += data + "\n"
        else:
            break
    # data = sys.stdin.read().strip()

    if entry:
        if input('Save entry? [Y]/n ').lower() != 'n':
            Entry.create(content=entry)
            print("Saved!")
        else:
            print("Your entry wasn't save")

And now it works well and helps me keep learning.

mykolash
mykolash
12,955 Points

Dude, you've saved me! Mega-thanx!!! Also occurred similar issue - Entry couldn't be saved. And I wanted to rewrite saving functional of this script, but decided to search for some solution. So, your code allowed me to keep being a bit lazy. Thanx again - it does work!

Luiz Cesar Merjan de Paula
Luiz Cesar Merjan de Paula
4,096 Points

Code

!/usr/bin/env python2

from collections import OrderedDict import datetime import sys

from peewee import *

db = SqliteDatabase('diarey.db')

class Entry(Model): content = TextField() timestamp = DateTimeField(default=datetime.datetime.now)

class Meta:
    database = db

def initialize(): '''Create the database and the table if they dont exist''' db.connect() db.create_tables([Entry], safe=True)

def menu_loop(): '''Show the menu''' choice = None

while choice != 'q':
    print("Enter 'q' to quit")
    for key, value in menu.items():
        print('{}) {}'.format(key, value.__doc__))
    choice = raw_input('Action: ').lower().strip()

    if choice in menu:
        menu[choice]()

def add_entry(): '''Add an entry''' print("Enter your entry. Press ctrl+d when finished.") data = sys.stdin.read().strip() print(data)

if data: #se existir data
    try:
        if raw_input("Save entry? [Y/N] ").lower() != 'n':
            Entry.create(content=data)
            print("Saved succesfully")
    except EOFError:
        print("save aborted")

def view_entries(): '''View previous entries''' entries = Entry.select().order_by(Entry.timestamp.desc())

for entry in entries:
    timestamp = entry.timestamp.strftime('%A %B %d, %I: %M%p')
    print(timestamp)
    print('='*len(timestamp))
    print(entry.content)
    print('N) next entry')
    print('q) return to main menu')

    next_action = raw_input('Action: [N/q]').lower().strip()
    if next_action == 'q':
        break

def delete_entry(): '''Delete an entry'''

menu = OrderedDict([ ('a', add_entry), ('v', view_entries),

])

if name=='main': initialize() menu_loop()