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
Elizabeth McInerney
3,175 PointsRunning diary.py in workspaces
I just finished the unit "Our Diary App" in "Using Databases in Python". I passed the challenge, but wanted to run the program in workspaces before moving on. When I did, I got a "no such file or directory" on the shebang line, and several lines of $'\r': command not found at the top of my code. Here is my code:
# we have to add a shebang line to run on workspaces or a mac
#!/usr/bin/env python3
from collections import OrderedDict
import datetime
from peewee import *
db = SqliteDatabase('diary.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 don't 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 = input('Action: ').lower().strip()
if choice in menu:
menu[choice]()
def add_entry():
"""Add an entry."""
def view_entries():
"""view previous entries"""
def delete_entry(entry):
"""delete an entry"""
menu = OrderedDict([
('a', add_entry),
('v', view_entries),
])
if __name__== '__main__':
initialize()
menu_loop()
1 Answer
Kenneth Love
Treehouse Guest TeacherI think the shebang has to come first and you have a comment first. I haven't tested this, though.
Elizabeth McInerney
3,175 PointsElizabeth McInerney
3,175 PointsIn your video, the # was the first item in the line, followed by the !/ etc.
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherYeah, the
#!is the shebang (hash + bang). Your first line is a comment about what your next line is, though. Swap line 1 and line 2.Elizabeth McInerney
3,175 PointsElizabeth McInerney
3,175 PointsReally? It matters where a comment line would go? I think of comment lines as being invisible. But I will give it a try.
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherWell, at that point, it's the shell reading your code, not Python, so, yeah it matters. IIRC, most shell code's comments start with
;, so it might not have problems with that kind of of comment but it's easier to just swap them around.