The Solution2:48 with Kenneth Love
Here's how I did it!
How was it? 0:00 Were you amazed at how soon you'll hit one of these milestones? 0:01 I know a couple caught me off guard. 0:03 Let's see how I broke this one down. 0:06 First, I got to get that name. 0:07 That's simple enough. 0:09 Just ask for their name and start on the variable. 0:10 We've all done that. 0:13 So getting the year they were born could be a bit trickier. 0:15 We have to make sure they gave us a number. 0:19 Now, I'm going to use a while loop, and 0:21 then I will have a try and accept inside of it. 0:24 Now there is probably a more clever way to solve this especially using something like 0:27 strings is numeric method. 0:31 But I don't mind doing things that a bit more spelled out like this. 0:33 It definitely shows you exactly what is going on. 0:36 So now that I have the year they were born, I can figure out how old they are. 0:39 I am going to do that by setting the current year to a variable. 0:43 Now that does mean I'm going to have to update the script next year, 0:47 but whatever, that's fine. 0:50 By then, I'll probably know a better way of doing this. 0:52 Right now when I'm recording this, it is the year 2017. 0:55 So that is what I am going to put for current year. 0:58 If you're watching this in the future, hi, and I hope things turned out well. 1:00 Then to get their age, 1:04 roughly, I'm going to subtract their birth year from the current year. 1:06 This doesn't really account for their birthday if it's passed yet or not, but 1:10 it's close enough for what we are doing. 1:15 Now I have to figure out when they are going to turn a particular age. 1:17 To do that I can take whatever age it is that I want to know when they 1:21 are going to turn. 1:26 I can subtract form that their current age. 1:27 And again, there is probably a more clever way of doing this. 1:31 But then I'm going to add it to the current year. 1:34 So to figure out when somebody if going to turn 25 for example, 1:38 I take 25 minus their current age and then I add that to the current year. 1:41 So, if they were say 30, this is going to give me negative 5 which means 1:45 that current year is going to be before 2017. 1:50 And we're going to use that in just a minute. 1:54 So I have all the dates that I need. 1:56 Now I just need to print things out. 1:58 I'll use an if to make sure that each year is greater than the current year. 2:00 And if it is, then I'll print out the message to the user. 2:05 So here's what that looks like. 2:08 If turn 25 is greater than current year, print out something. 2:10 Turn 50, so on and so forth. 2:13 It's time to try this one out. 2:16 So we'll do python age_calc.py. 2:17 You can call me Kenneth. 2:20 And I was born in 1981. 2:24 So there we go. 2:25 I didn't get the 25 because I'm past that one. 2:27 But I still got 50, 75, and 100. 2:31 Nice. I hope you liked this 2:34 bit of extra practice. 2:35 Be sure to fill out the feedback survey about these kinds of practices and 2:37 let us know what else you'd like to practice in the future. 2:40 If you have ideas for other types of practice, be sure and 2:42 let us know about those, too. 2:45 Thanks and I'll see you next time. 2:46
You need to sign up for Treehouse in order to download course files.Sign up