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
Bryan Park
13,481 PointsMy solution with the current year calculated for me
Kenneth Love , I thought I would share my solution of the Basic Math workshop. Especially since I did include the capability of the app calculating the current year for me, right at the start of step 3.
from datetime import date
# Step 1
# Ask the user for their name and the year they were born.
name = input("Please enter your name: ")
while True:
birthyear = input("What year were you born? ")
try:
birthyear = int(birthyear)
except ValueError:
continue
else:
break
# Step 2
# Calculate and print the year they'll turn 25, 50, 75, and 100.
age25 = birthyear + 25
age50 = birthyear + 50
age75 = birthyear + 75
age100 = birthyear + 100
# Step 3
# If they're already past any of these ages, skip them.
currentYear = int(date.today().strftime("%Y"))
if age25 > currentYear:
print ("{}, you will turn 25 in {}, 50 in {}, 75 in {}, and 100 in {}".format(name, age25, age50, age75, age100))
elif age50 > currentYear:
print ("{}, you will turn 50 in {}, 75 in {}, and 100 in {}".format(name, age50, age75, age100))
elif age75 > currentYear:
print ("{}, you will turn 75 in {}, and 100 in {}".format(name, age75, age100))
elif age100 > currentYear:
print ("{}, you will turn 100 in {}".format(name, age100))
else:
print ("{}... Damn! You're old!".format(name))
Although we were close to doing it the same way and very different in others, I really like the last line of my code (although most likely it will never be used).
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsNice code. The integer year can be accessed directly without having to convert to a string then back to an int. The method date.today() returns a date-object that has year, month, and day available:
>>> import datetime
>>> date.today().year
2018