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 CRUD: Create Function

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Create a function named create_challenge() that takes name, language, and steps arguments.

Hello.

So, we want to create this function that takes 3 arguments and creates the Challenge using those 3 arguments and I am stuck:

from models import Challenge

def create_challenge(name, language, steps):
  Challenge.create

This is where I am stuck, I have defined the function and I presume it is ok, but I do not know how to have the content of the Challenge that we need to create from to all the 3 arguments, how do we write this?

I have tried to look at the peewee docs but couldn't come up with a solution.

Any help?

Thanks

Vittorio

crud.py
from models import Challenge

def create_challenge(name, language, steps):
  Challenge.create
Anthony Twietmeyer
Anthony Twietmeyer
661 Points

I'm with you Vittorio. Did the answer given give you any help. It didn't give me any help. Still stuck.

6 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

We use .create() to create a Student in the course. Do the same thing to create a Challenge in the challenge.

Anthony Twietmeyer
Anthony Twietmeyer
661 Points

Yes, indeed, in an earlier exercise we used the format for supplying the arguments for creating a Student. The peewee documentation doesn't seem to show the data being passed on separate lines. Is that a requirement?

Thanks!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

No, the separate lines aren't required.

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Thanks everyone for helping out.

I have passed the challenge with the following code:

from models import Challenge

def create_challenge(name, language, steps):
  Challenge.create(name=name, language=language, steps=steps)

But I am not sure about the steps part: Steps should be optional, so give it a default value of 1.

I have basically ignored it, I was setting steps=1 on the last line of code as I thought that was the way to do it, but I passed with steps=steps which I am not 100% understanding as we should set the steps to 1 ..

I am not 100% sure the code I finally came out with is 100% correct..

Or am I wrong?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

How do you make an argument to a function optional? In other words, you need to provide a default value for the argument. We've done that in at least 2 other courses.

EDIT:

And, other than that, your code looks great!

This code worked for me:

from models import Challenge

def create_challenge(name, language, steps=1): Challenge.create(name=name, language=language, steps=steps)

Without directly giving the answer

From the peewee documentation:

user = User.create(username='admin', password='test')

"User" being your model, and of course the values of each field pertaining to the arguments passed to the function. Hope this helps.

https://peewee.readthedocs.org/en/latest/peewee/api.html?highlight=create#Model.create

Anthony Twietmeyer
Anthony Twietmeyer
661 Points

Vittorio, look ahead to the next 'exercise' to see how the coding looks.

Stéphane Diez
Stéphane Diez
19,350 Points

steps making optional 1 is like this:

def function(steps=1)

right ?

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Thanks Kenneth.

Had some dust in my brain on this. I have now added the correction inside the arguments and checked it in the code challenge.

Much appreciated!

Vittorio