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 Dates and Times in Python (2014) Dates and Times strftime & strptime

Dante Vittorelli
Dante Vittorelli
2,301 Points

Getting an error stating that the correct answer is wrong.

For some reason I am getting the error " Bummer! to_string returned the wrong string. Returned '24 September 2012'."
I don't understand why because the challenge is asking for exactly that date, yet it's not passing. Would anyone know why this is happening?

import datetime

def to_string(x):
    x = datetime.datetime.now()
    x = x.replace(year = 2012, month = 9, day = 24)
    z = x.strftime('%d %B %Y')
    return z

[MOD: added ```python markdown formatting -cf]

Dante Vittorelli
Dante Vittorelli
2,301 Points

Sorry, I clicked the checkbox that says "attach code to post" but I guess it didn't attach it.

import datetime

def to_string(x):
    x = datetime.datetime.now()
    x = x.replace(year = 2012, month = 9, day = 24)
    z = x.strftime('%d %B %Y')
    return z

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The error is your are overwriting the argument x with your own value.

import datetime

def to_string(x):
    #x = datetime.datetime.now()
    #x = x.replace(year = 2012, month = 9, day = 24)
    z = x.strftime('%d %B %Y')
    return z

This should work for Task 1

Dante Vittorelli
Dante Vittorelli
2,301 Points

Oh wow... my answer makes no sense now that I get it haha. Thank you for your help.