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

What is this challenge asking me to do?

I have been doing in the challenges, as I am here, only what is requested at each step. That seems to be the pattern when completing challenges. So, I created a function but I don't understand fully what I should be doing here. First, I got stuck with it telling me that challenge was not defined. Then it tells me that list indices must be integers and not str. So, I'm not sure how to pull that off.

I feel like I am missing some information or that perhaps some additional steps are required but which were not directly requested as a requested steps to perform.

Thanks, Bruce

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Can you post your code? (look for posting help in the Markdown Cheatsheet below the text box.)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Challenge is defined since it is imported within the challenge: from models import Challenge. Note it's capitalized. Is your code referring to the lowercase "challenge"?

The challenge task 1 asks:

Create a function named create_challenge() that takes name, language, and steps arguments. Steps should be optional, so give it a default value of 1. Create a Challenge from the arguments. create_challenge should not return anything.

Breaking this down:

# Create a function named create_challenge()
def create_challenge():
     pass #<-- nothing happening yet

# ...that takes name, language, and steps [as] arguments...
def create_challenge(name, lang, steps): #<-- add arguments
     pass

# ...Steps should be optional, so give it a default value of 1....
def create_challenge(name, lang, steps=1): #<-- add default of 1 to steps argument
     pass

# ...Create a [new instance of] Challenge from the arguments [provided]....
def create_challenge(name, lang, steps=1): #<-- add default of 1 to steps argument
    Challenge.create(
        name=name,
        language=lang,
        steps=steps)

# create_challenge should not return anything. --> don't add 'return' statement

I guess what confused me was the fact that during the previous training video, the class, which was capitalized, corresponded to a database Model. In the training video, we had the Model definition inside the same file. In addition, while completing the Challenge or quiz, by not including the model in the file and not asking me to create the Model in the quiz, I am not able to see the model definition. For me, when I import a module, I want to know and be able to reference the API, as it were.

It was either on this question or another one, where I submitted my answer and Python said "I need integer indices and not str indices. It would help to have a reference to the Challenge class, so I could see what is being imported. I hope my feedback makes sense. Thanks, Bruce

Chris Grazioli
Chris Grazioli
31,225 Points

Dumb question, but how or why does steps=1 signify the argument being optional. Was this is a previous video somewhere? It seems as though that was pulled out of thin air, but then again I have a bad memory especially if its not something I'm using over and over again. Is there a quick and easy documentation to read up on. I try to look at the docs but searching stuff like that seems to always drag you through a large amount of stuff your not looking for.

Chris Grazioli
Chris Grazioli
31,225 Points
def create_challenge(name, lang, steps=1): #<-- add default of 1 to steps argument
    Challenge.create(
        name=name,
        language=lang, #<--Why the change here?
        steps=steps)

Just out of Curiosity? Why the different naming convention when making the object. I spelled out language and the code challenge accepted it! I also ran it your way noted here and it worked as well.

Is this just something you like to do, I only ask because the challenge specifically stated " name, language, steps". I'm guessing if you ran something like:

def create_challenge(name, lang, steps=1): #<-- add default of 1 to steps argument
    Challenge.create(
        name=name,
        lang=lang, #<--Why not the change here?
        steps=steps)

It probably wouldn't accept it because the Model is different. Am I correct in assuming that?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Chris Grazioli, good questions!

> how or why does steps=1 signify the argument being optional

The presence of a the default value =1 indicates that if a third parameter is not supplied to the function, the local variable steps will be assigned to 1.

> Why the different naming convention when making the object.... Is this just something you like to do, I only ask because the challenge specifically stated " name, language, steps".

While at first it looks like my coding was lazy or sloppy, it raises a valid point: positional argument names can be anything you wish and the code will still work. Saying this another way, the task description says "create a function... that takes name, language, and steps as arguments" means the function will be called with arguments referencing objects for a name, a language, and optionally a step value, but the receiving parameter names do not literally need to match. The function will "take it" but it can call the local reference whatever it wants. The parameter names become the local variable references to the passed arguments.

As for the arguments in the call to Challenge.create(), "name", "language", and "steps" are keyword arguments for the create method and must match the attributes of the Challenge class.

This will also pass the challenge:

def create_challenge(thing1, thing2, thing3=1):
    Challenge.create(
        name=thing1,
        language=thing2,  # <-- "language" must match class attribute 
        steps=thing3)
Chris Grazioli
Chris Grazioli
31,225 Points

When you use the word "optional" to describe the argument passed to the function, specifically the "steps" =1 this isn't really optional is it. It's more of a Defaulted value, which the user has the "option" to change?

Say for instance you wanted to instantiate an object or use a function, but you wanted the argument to exist or not exist, but maybe not be required by the user to pass into the function, what would you do? Set the default value to none or something?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

It's optional in the sense that it is not required to be specified in the function call. Your idea of calling it a default value the caller can change is a valid perspective.

Setting a parameter default to None is used often. The body of the function can then test for its value:

def some_func(thing1, thing2, thing3=None):
    if thing3:
        # code related to thing3
    # remainder of the function code