Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 3: Birthday countdown app
Lecture: Sketching the program flow

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Over here in PyCharm, let's start working on our birthday app. So as usual, we are going to come over here
0:06 and pick a new Python file, and call it program. Now we are going to start out by having a header, then the next thing we are going to do is
0:14 we are going to ask the user for their birthday information, and then we are going to compute the days between now and that birthday they entered,
0:23 and then finally we'll tell them whether their birthday is coming up how many days, that sort of thing.
0:29 So we could just write this out in one giant series of events, but I find it much easier to think about this in small little steps,
0:37 kind of like I just described, so we can do that with functions. So we are going to start by actually defining a function
0:44 and the purpose of this function is to get the input from the user and return it any time that we need it. So let's say get_birthday_from_user().
0:55 So remember, to define a function it always starts with a keyword def, and then the function name, any parameters
1:01 if it takes none, you just have open close parenthesis and a colon. And then you indent to define the code block that is the function body.
1:08 Notice when I hit enter, PyCharm automatically took care of that for me. Now, one thing we can do is we can just kind of sketch this out
1:14 and if I want to define the function but not really implement it or write the details of it here, I can just say pass, right?
1:21 So the other thing that we are going to do is we are going to compute the days between two dates so we'll say compute_days_between_dates(),
1:31 all right, and all right pass again just so we can go on and sketch out the rest of the app. Now notice, PyCharm has put a little squiggly into here
1:39 and there is a warning that says this is a pep 8 violation, now pep 8 is the standard for how code should be written in Python,
1:48 you don't have to follow it but pretty much everybody does for the most part, and, one of the rules in pep 8 is that
1:55 when you have two functions that are stand alone functions, there is supposed to have two lines between them.
2:01 PyCharm actually knows how to fix many of these and I can hit command+alt+L and it will do whatever it needs to fix it.
2:08 Now the next thing we are going to do first we are going to get the information from the user, we are going to compute the days
2:14 and then we are going to say def print_birthday_information(), something like that. And again I am going to pass... and there is one part
2:21 I kind of omitted because I don't really think of it as an operation, but it is something we are going to do, is we are going to print the header,
2:29 and let's just put pass really quick. So that is kind of the steps of our app there we are going to print out the hello,
2:34 welcome, get the information from the user about their birthday, compute whether that is in the past or the future,
2:40 how many days and then we are going to use that to print out the information.
2:44 The last thing to do is actually just orchestrate this into a main method, so we'll define a main method,
2:50 here some programming languages like C++, C# and so on, they require you to actually define a main method called main,
2:59 there is nothing like that in Python, this is just a convention. So, over here, we are going to first start
3:04 by printing the header and then we are going to get the birthday information from the user, we are going to store this in a variable
3:12 but just hold on for a minute there, we are going to compute the days between the dates,
3:17 and then finally, we are going to print the birthday information. And then we'll be done.
3:23 Final thing is we are going to be returning some information from this, we are actually going to return a date object
3:28 that represents the real birthday, so we'll call this b-day here, and this birthday we are going to need to pass off to this function,
3:36 so we have the birthday, and we are also going to need to know what time it is right now and I am just going to put none
3:45 because I don't really want to talk about that what that is just this moment, but we are going to get to that right away,
3:51 so we are going to call get birthday from user, get their birthday, we'll compute what the time is now, we are going to compare the times between these
3:58 and this is going to actually give us number of days, like so, and then we are going to pass this on here and we will print this out.
4:08 So you can see that PyCharm is actually showing us that hey, you are trying to use the return value of this function,
4:15 and over here you can see we just say pass they are not actually returning the value. So our next job is to go and implement
4:21 each one of these functions, so this flow works as we expect.


Talk Python's Mastodon Michael Kennedy's Mastodon