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 trialAshley Keeling
11,476 PointsI am not sure why this isn't working
i don't know what i need to do, to make it work
import datetime
## Examples
# to_string(datetime_object) => "24 September 2012"
# from_string("09/24/12 18:30", "%m/%d/%y %H:%M") => datetime
def to_string(datetime):
now = datetime.datetime.now()
unites = input("")
unites = unites.spilt("-")
awnser = []
if unites == "%d":
awnser.apped(now.day)
elif unites =="%B":
awnser.append(now.month)
elif unites == "%Y":
awnser.append(now.year)
awnser.join("")
1 Answer
Unsubscribed User
6,415 PointsI'll try to offer a little help just from a glance to point you in the right direction as to why nothing is working here:
You're passing
datetime
as an argument to yourto_string()
function when you've already imported the datetime module. So in other words, if you're really intending to usedatetime
as an argument name in this function AND use thedatetime
module within this function, you might end up with some unexpected results.A few of your methods are misspelled, namely
.spilt()
and.apped()
on lines 10 and 14 respectively.The formatting codes like
%d
,%B
, etc. have no meaning outside of specific methods ofdatetime
objects. Usually, these are used in methods like.strftime()
and.strptime()
but as standalone strings like you have in your conditions here, they have no meaning whatsoever.You don't return your
awnser
list at the end of the function so you can't get anything back out of the work this function does.
Judging from your comments though, I think what you're attempting to accomplish here can be as simple as the following:
import datetime
def to_string(dt_object):
str_date = dt_object.strftime("%d %B %Y")
return str_date
def from_string(str_date):
dt_object = datetime.datetime.strptime(str_date, "%d %B %Y")
return dt_object
now = datetime.datetime.now()
now_string = to_string(now)
# now_string is now "21 January 2018"
now_datetime = from_string(now_string)
# now_datetime is now a datetime.datetime(2018,1,21,0,0) object
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsMoved comment to answer. Xander Morrison you've been added great answers as comments. I suggest you add them directly as answers instead! Thanks for your help in the community forum!
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsReference link to strftime documents
Ashley Keeling
11,476 PointsAshley Keeling
11,476 Pointsthanks for the help but i dont fully get why you have called the argument 'dt_object' (the name of it)
thanks
Unsubscribed User
6,415 PointsUnsubscribed User
6,415 PointsYou don't have to call it
dt_object
, that's just the parameter name I give to the function so that I know when I call that function, I need to pass to it adatetime
object, in this casenow
since I definenow = datetime.datetime.now()
. In that function, I use.strftime()
on the parameter you pass to the function..strftime()
is a method of adatetime
object so what you pass toto_string()
must be adatetime
object as well. Otherwise, this will throw an error. So to summarize, you put adatetime
object into theto_string()
function, and you get the formatted string representation of that object out. And you put a string into thefrom_string()
function, and you get a datetime object out of the function Hope this helps!Ashley Keeling
11,476 PointsAshley Keeling
11,476 Pointsthanks i get it now
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsIt is not a good idea to use built-in Python functions or imported modules names as variables names. In this case, using
datetime
as the literal variable name prevents accessing any other functions from the datetime module:This happens because "datetime" in the local namespace within the function is now set to the argument object passed in which has it's own methods but no knowledge of the other functions within the
datetime
module.