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 trialjessicakincaid
21,834 PointsI'm stuck in the datetime strings challenge. I get the right result in my workspace, but my solution is incorrect.
I made a new file and tested this line by line in my workspace, and it returned the right result there. Surely it's something obvious. Thank you in advance for your help!
import datetime
"""Create a function named to_string that takes a datetime
and gives back a string in the format "24 September 2012"."""
def to_string(datetime_object):
datetime_object = input('Please enter a date as (MM-DD-YY)')
object_digits = datetime.datetime.strptime(datetime_object, '%m-%d-%y')
return object_digits.strftime('%d %B %Y')
```timestrings.py
## 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(datetime_object):
datetime_object = input('Please enter a date as (MM-DD-YY)')
object_digits = datetime.datetime.strptime(datetime_object, '%m-%d-%y')
return object_digits.strftime('%d %B %Y')
5 Answers
Jeff Muday
Treehouse Moderator 28,720 PointsFred Gablick is correct, the automated grader uses Python's unittest
library, thus it automatically feeds the input and expects a particular output. Using Python's input()
method will keep the Challenge automated grader from working properly.
Also... this challenge is designed for the student to recognize that datetime's strftime
and strptime
are each other's inverse.
Jessica did a good job of recognizing how part 1 should be coded. Nice work! I show a slightly simpler way to do part 1 below which avoids the explicit creation of an extra datetime object.
I left part 2 of the challenge for Jessica to solve.
Here is the official Python document on strptime
and strftime
linked below
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
import datetime
def to_string(datetime_object):
# part 1 of challenge: return a string from datetime_object
return datetime.datetime.strftime(datetime_object, '%d %B %Y')
def from_string(date_string, date_format):
# part 2 of challenge: return a datetime object from a date_string and a date_format
... fill in your code here ...
Fred Gablick
5,840 PointsThe first thing that jumped out is that you're defining your datetime_object twice - once as the function parameter, and then again inside the function. It works fine for me if I move the input line outside of the function:
import datetime
"""Create a function named to_string that takes a datetime
and gives back a string in the format "24 September 2012"."""
def to_string(datetime_object):
object_digits = datetime.datetime.strptime(datetime_object, '%m-%d-%y')
return object_digits.strftime('%d %B %Y')
datetime_object = input('Please enter a date as (MM-DD-YY)')
print (to_string(datetime_object))
*Disclaimer - I don't know the lesson to which you refer, I just saw that and wanted to bring it to your attention
jessicakincaid
21,834 PointsThank you, Fred Gablick. It's encouraging to know that the code worked for you after you moved the input statement! I removed it but my answer was still incorrect. After "Bummer!" the checker elaborated further:
"Exception: We caught an EOFError exception! Do you have a call to input() in your code? Sorry, but the input() function doesn't work in code challenges! Please remove the call to input(), and try again." The code challenge is called "strftime and strptime" and is the first of a two-part challenge in the Python Dates & Times course.
Fred Gablick
5,840 PointsAhh! Have you tried taking the input() line completely out of your challenge code? A lot of the challenges aren't looking for complete, working code, and know how to check for exactly what they ask.
jessicakincaid
21,834 PointsYou are right on. I finally received a correct answer after I brought the function down to two lines, with no input() line. It seems to me that the checker is designed to make us write as minimal code as possible. Have you read "The Zen of Python" by Tim Peters? I learned about it in Kenneth's Write Better Python course. It's there if you open a Python shell open and type
>>> import this
Fred Gablick
5,840 PointsI'm embarrassed that I had NOT seen that before; thanks for sharing. I'll have to admit - I disagree with a lot of it, but overall it's good advice.
I should also admit that I struggle with making my code "Pythonic," so take my disagreement with a grain of salt. I have been thinking more and more that I need a mentor.
jessicakincaid
21,834 Pointsjessicakincaid
21,834 PointsI only submitted the function once, I formatted it above for sharing and it displayed twice.