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 trialBen Thomas
1,911 PointsChallenge Code Works Returns Correct Answer in IDLE Shell, But Not on Website
My code is as follows:
import datetime
now = datetime.datetime.now()
two = now.replace(hour=14)
two.replace(minute=30)
In IDLE Shell, this returns "datetime.datetime(2014, 12, 2, 14, 30, 35, 903807)," which shows that the hour has been changed to 14 and the minute to 30.
However, in the online code editor, the same code keeps returning my computer clock's current minute as datetime's minute. Any ideas on why IDLE and Treehouse are giving me two different answers?
import datetime
now = datetime.datetime.now()
two = now.replace(hour=14)
two.replace(minute=30)
3 Answers
Kenneth Love
Treehouse Guest Teacherdatetime.replace()
gives back a new datetime
, it doesn't change the datetime
in place. You need to assign your last step back to the variable two
again.
Ben Thomas
1,911 PointsThat worked! But I'm still not sure I understand why the exact same code changes the minute in IDLE, but not in the Treehouse editor. Could you explain this?
Kenneth Love
Treehouse Guest TeacherIDLE is...weird. It's not 100% a text editor and it's not 100% a REPL. Likely it was showing you the last computed value, which would have been correct, instead of actually showing you the variable's current value. I can't answer this for certain as I don't use IDLE very much.
james white
78,399 PointsKenneth said: "You need to assign your last step back to the variable two again."
So I thought this would work:
import datetime
now = datetime.datetime.now()
two = now.replace(hour=14)
two = now.replace(minute=30)
..but it doesn't pass.
I ended up passing with this:
import datetime
now = datetime.datetime.now()
two = now.replace(hour=14, minute=30)
Don't really understand why?
but moving on..
Kenneth Love
Treehouse Guest TeacherYou reset two
to be equal to now
but with the minute
attribute set to 30. You didn't, however, continue to change two
. Doing it the way you did in your solution is fine, or you could just reassign two
to itself, again, this time changing the minute
attribute.
two = now.replace(hour=14)
two = two.replace(minute=30)