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 trialAva Jones
10,723 PointsI do not understand the instructions
please help me!
## Examples
# to_string(datetime_object) => "24 September 2012"
# from_string("09/24/12 18:30", "%m/%d/%y %H:%M") => datetime
import datetime
def to_string(date):
return date.strftime("%d %B %Y")
def from_string("2014 12 2", strftime):
return "2014 12 2" + strftime
2 Answers
Asher Orr
Python Development Techdegree Graduate 9,410 PointsHi Ava! You completed the first challenge successfully.
import datetime
def to_string(datetime):
return datetime.strftime("%d %B %Y")
Your code takes a datetime object as an argument, and it returns a string. Perfect!
The second challenge asks for a function that takes 2 arguments:
- a date as a string
- an strftime-compatible format string
.. and returns a datetime created from them.
In your code, you passed in an actual string as the first argument:
def from_string("2014 12 2", strftime):
Remember that arguments are kind of like placeholders. You're saying to Python:
"I'm creating this function now. When I call it later, I'm going to pass in arguments.
I'm not sure what they'll be yet, but the first argument will be a date as a string. The second argument will be an strftime-compatible format string."
Therefore, it should look like this:
def from_string(date_as_string, strftime_formatted_string)
When you click "Check Work", the Treehouse code challenge is going to call your function. It will pass in a date as a string. Then it will pass in a string that works as an strftime-compatible format.
The challenge might run your code like this:
from_string("09/24/12 18:30", "%m/%d/%y %H:%M")
Now, you need to return a datetime object created from your arguments- both of which are strings.
This means you need to use the strptime method. Kenneth covers this in the "Dating Methods" video: https://teamtreehouse.com/library/dates-and-times-in-python/dating-methods#transcript
Let me know if you still don't understand. I hope this helps!
Steven Parker
231,275 PointsThe second challenge is similar to the first in that you will need to call a function from the datetime library, using the arguments that came in to your function, and return the result. You still need to identify the correct function and use it.
But you also have a syntax issue where you have used a string literal ("2014 12 2") as a parameter name. Parameter names must be chosen following the same rules as variable names.