Wikipedia Links4:35 with Kenneth Love
Here's my solution to the Wikipedia app homework.
Well, you came back. 0:00 So, the challenge must not have been too intense. 0:01 That's good. I like students with 0:03 a feisty edge to them. 0:05 All right. 0:05 Lets hop into workspaces, and see how I solved this. 0:07 There's a workspace you can use that's associated with this video, 0:09 if you just want to look at my code, and mess with it. 0:12 All right, so I've created this file wiki.py. 0:14 And, since we're working with DateTime, obviously, I'm going to import DateTime. 0:18 I think I wanna set up a couple of variables first, 0:23 to kinda hold onto some things. 0:26 I wanna hold onto how the dates are formatted, and 0:28 I need to format the date and the stuff for Wikipedia. 0:31 And, I have to be able to format the URL for Wikipedia. 0:34 So, let's do answer_format, and I'm gonna present, 0:37 I'm gonna expect this to come in as month as two numbers, and date as two numbers. 0:41 And, then my link format, which is how Wikipedia wants it, is going to be %b, 0:48 so this is the month name, in the locale, _%d. 0:53 So, I'll get back like Jan 01, right? 0:59 And, the link is actually https, and .wikipedia.org/wiki, 1:02 and then we're gonna put in a format thing down there. 1:10 So, if you're doing this in German, or Chinese, or 1:14 whatever, first of all, thank you for listening, cuz that's awesome. 1:19 Second of all, you're gonna have a different URL there. 1:22 If you need to change that, feel free, go for it, and everything should be good. 1:26 All right, I think now, I think we're ready for a loop. 1:30 So, if you just wanted this to run once, 1:33 then you'd want to leave out the loop, but I want to let this running multiple times. 1:35 So while true, answer = input("What date would you like? 1:41 Please use the MM/DD format. 1:49 Enter quit to quit. 1:54 And, you all that have done my courses several times know that this 1:58 is nothing new. 2:01 If answer.upper is equal to QUIT, then we wanna call break. 2:03 Okay. We're going to ask our question. 2:10 We give them a way to drop out if they want to. 2:13 So now, let's try and, make a new datetime object here. 2:16 So, datetime.datetime.strptime. 2:20 And, we're gonna do answer, and answer format. 2:24 So, this is gonna make a datetime for us. 2:29 It'll default to, like, this year and stuff. 2:32 It'll, it's gonna work. 2:35 It's gonna convert their answer into our date. 2:35 And then, let's say link.format, date.strftime, link format. 2:38 So, we're going to format our link string 2:45 using the date turned back into a string based on this link format, 2:48 from the date that we created, with the string they sent in. 2:52 Several steps, but I think we'll, we'll be okay. 2:55 And then, let's print the output. 2:57 But, if they gave us something that can't be turned into a date, 3:00 it's gonna throw a value error. 3:03 So, let's do accept value error. 3:05 We're gonna print that's not a valid date. 3:08 Please try again, okay. 3:12 If they give us something that doesn't match, give them back this message. 3:15 All right. Let's give this a try. 3:19 Let's let's see what happens here. 3:20 So, we come down here, and we do python wiki.py. 3:23 What day would you like to use? 3:27 Okay, first of all, let's try using a bad date. 3:27 There is no 13th month with a 45th day. 3:30 That's not a valid date. 3:34 Okay, cool, great. 3:35 Let's do December 25th. 3:37 So, there's our link for December 25th. 3:39 Let's try, I don't know, May 31st. 3:42 There's one from May 31st. 3:45 And, lets try January 1st. 3:46 All right, so now lets just copy one of these. 3:49 We'll grab this May one, and copy it. 3:52 And, we will hop over here to this datetime window, and then we go to it. 3:55 And, look, it loads up, and there we go. 4:00 It's the 151st day. 4:03 And, here's all the stuff that happened on that day. 4:06 So, that's pretty awesome. 4:10 That seems to be working exactly how we wanted it to work. 4:12 Congratulations, you now have a good foundation for working with dates and 4:16 times in Python. 4:19 See if you can modify your Wikipedia script to take a list of dates, and 4:21 return a list of a links. 4:24 In our next stage, we're gonna build a quick app to ask us a bunch of math 4:26 questions, and then tell us how long it took to answer them. 4:29 This will be a good test of our datetime and OO skills. 4:32
You need to sign up for Treehouse in order to download course files.Sign up