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
james white
78,399 PointsUsing Databases in Python Initialize Database and Create tables Challenge
Challenge Link:
I got through the first two challenges:
Challenge Task 1 of 3
Create a variable named db that is an SqliteDatabase with a filename of challenges.db.
Challenge Task 2 of 3
Now add db as the database attribute in the Meta class for Challenge.
..with:
from peewee import *
db = SqliteDatabase('challenges.db')
class Challenge(Model):
name = CharField(max_length=100)
language = CharField(max_length=100)
steps = IntegerField(default=1)
class Meta:
database = db
Here's final part I'm having trouble with:
Challenge Task 3 of 3
Finally, create a function named initialize. initialize() should connect to the database and then create the Challenge table. Make sure it creates the table safely.
Looking at the diary.py in the projects zipped files I'm seeing:
def initialize():
"""Create the database and the table if they don't exist."""
db.connect()
db.create_tables([Entry], safe=True)
Modifying this to meet the challenge requirements, the code should look something like:
from peewee import *
db = SqliteDatabase('challenges.db')
class Challenge(Model):
name = CharField(max_length=100)
language = CharField(max_length=100)
steps = IntegerField(default=1)
class Meta:
database = db
def initialize.initialize()
"""Create the database and the table if they don't exist."""
db.connect()
db.create_tables([Challenge], safe=True)
Unfortunately, when I paste this is it says:
Oops! It looks like Task 1 is no longer passing.
Is this an indentation issue?
In in Python it's more important than other languages..
Or it a code issue?
I noticed that 'initialize. initialize()' has a space after the period, but the challenge engine doesn't seem to like using that at all!
The course is so new (at this point) there aren't many thread connected with the syllabus:
5 Answers
shezazr
8,275 Pointsfew questions/comments:
- def initialize.initialize() does not have a : at the end which a function needs
- can you have function within a function?
- why are some variables/functions first letter capitalized? i.e. CharField, I would have expected charField?
- Finally do look at the preview screen to see what errors you are getting
james white
78,399 PointsHi Beven,
Good catch.
The "i" for 'initialize()' at the start of the second sentence in the challenge question was not capitalized so I missed this.
That's why I tried using:
def initialize.initialize()
With this corrected --gives this code instead:
def initialize()
"""Create the database and the table if they don't exist."""
db.connect()
db.create_tables([Challenge], safe=True)
It sill gives me the same error message:
Oops! It looks like Task 1 is no longer passing.
If I press the button to "Go back to task 1", it says:
Bummer! SyntaxError: def initialize()
It was shez azr who mentioned using a colon and
that was the other correction was needed!
The final code:
from peewee import *
db = SqliteDatabase('challenges.db')
class Challenge(Model):
name = CharField(max_length=100)
language = CharField(max_length=100)
steps = IntegerField(default=1)
class Meta:
database = db
def initialize():
db.connect()
db.create_tables([Challenge], safe=True)
Kenneth Love
Treehouse Guest Teacherdef initialize.initialize() <- what's this?
beven nyamande
9,575 Pointshad the same issue at first till i noticed the full stop demarcating end of line lol
Kenneth Love
Treehouse Guest TeacherI've updated the prompt a bit to make the "initialize(). initialize()" less confusing (sorry about that).
Glad you figured it out, james white
Ruu Mouat
14,122 PointsIf I can pitch in - looks like the 'Task 1 is no longer passing message' was because your docstring was over-indented. Remember, Python loves whitespace.
Therefor, the following is incorrect:
def initialize():
"""Create the database and the table if they don't exist."""
db.connect()
db.create_tables([Challenge], safe=True)
Whilst this should work just fine:
def initialize():
"""Create the database and the table if they don't exist."""
db.connect()
db.create_tables([Challenge], safe=True)
Should work just fine.
Off the top of my head, Kenneth covers whitespace in here: http://teamtreehouse.com/library/write-better-python
So maybe check that out if you haven't already! :-)
~Ruu
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest Teacher#2 - Yes, you can have a function inside of a function.
#3 - Those are classes so they start with capital letters.
MUZ141095 Kelvin Chawora
9,348 PointsMUZ141095 Kelvin Chawora
9,348 PointsIn response to your question (#2)....Yes, its very much possible to create a function within a function. I think they're called meta functions, if i'm not mistaken