1 00:00:01,180 --> 00:00:04,282 You might have noticed that some functions actually return a value. 2 00:00:04,282 --> 00:00:07,206 When you call the function len and pass it an object, 3 00:00:07,206 --> 00:00:09,212 it actually gives you a value back. 4 00:00:09,212 --> 00:00:11,870 In our case it gave us the number of characters in our string. 5 00:00:12,920 --> 00:00:15,850 Another example is when I call the upper method on a string, 6 00:00:15,850 --> 00:00:18,650 it created us a new string and returned it. 7 00:00:18,650 --> 00:00:21,150 Remember methods are really just owned functions, 8 00:00:21,150 --> 00:00:23,260 our string owns that function upper. 9 00:00:24,270 --> 00:00:28,670 So another great feature of functions is you can take some arguments, process them, 10 00:00:28,670 --> 00:00:31,510 and then return a value to the caller of your function. 11 00:00:31,510 --> 00:00:33,598 So let's create a function that returns a value. 12 00:00:33,598 --> 00:00:37,220 Let's see, I know, 13 00:00:37,220 --> 00:00:40,430 there are about a thousand apps that do something similar to this. 14 00:00:40,430 --> 00:00:42,430 You know when you're out to dinner with a bunch of people and 15 00:00:42,430 --> 00:00:44,760 you need to split the check up between each other? 16 00:00:44,760 --> 00:00:47,360 Let's write a function that takes the total of the bill and 17 00:00:47,360 --> 00:00:50,430 the number of people and then returns the value that each person owes. 18 00:00:51,600 --> 00:00:56,336 Okay, so let's create a new file and we'll call our app, call the script, 19 00:00:56,336 --> 00:00:58,344 let's call it check_please. 20 00:00:58,344 --> 00:00:59,130 That's a good name. 21 00:01:01,061 --> 00:01:04,270 All right, and let's start defining our function. 22 00:01:04,270 --> 00:01:07,463 So let's see, names are hard. 23 00:01:07,463 --> 00:01:12,966 How about we define a new function called split_check. 24 00:01:12,966 --> 00:01:17,570 All right, and we're gonna need to define a couple of parameters, right. 25 00:01:17,570 --> 00:01:22,080 So let's see, we'll need the total of the bill, so we'll call that total. 26 00:01:23,090 --> 00:01:27,940 And we're also gonna need to have the number of people involved in paying 27 00:01:27,940 --> 00:01:29,160 this bill, right? 28 00:01:29,160 --> 00:01:32,390 So to define multiple parameters for a function, 29 00:01:32,390 --> 00:01:36,990 you just separate them with a comma, and then you just keep declaring. 30 00:01:36,990 --> 00:01:42,230 So let's see, that's number_of_people. 31 00:01:42,230 --> 00:01:44,200 I think that's all we'll need, right? 32 00:01:44,200 --> 00:01:48,200 And then we don't wanna forget the colon to signify that the body of our function 33 00:01:48,200 --> 00:01:48,925 is coming up. 34 00:01:48,925 --> 00:01:51,752 So how do we calculate this? 35 00:01:51,752 --> 00:01:55,652 I guess if we just divide the total by the number of people, 36 00:01:55,652 --> 00:01:58,370 we should be pretty good, right? 37 00:01:58,370 --> 00:02:00,406 So let's store that in a new variable. 38 00:02:00,406 --> 00:02:05,649 So we'll say cost per person, Is 39 00:02:05,649 --> 00:02:11,130 equal to the total / number_of_people. 40 00:02:11,130 --> 00:02:12,020 That seems to make sense. 41 00:02:13,068 --> 00:02:17,740 So this cost_per_person value is what we want to give back 42 00:02:17,740 --> 00:02:20,060 to whomever called this function. 43 00:02:20,060 --> 00:02:23,050 So we want to return this value. 44 00:02:23,050 --> 00:02:24,830 So, not surprisingly, 45 00:02:24,830 --> 00:02:29,790 the key word to return a value from within a function is, return. 46 00:02:30,790 --> 00:02:34,710 So, what you do is you say return and then the value. 47 00:02:34,710 --> 00:02:38,597 So we're gonna return cost_per_person. 48 00:02:38,597 --> 00:02:43,000 So now, let's use our new function. 49 00:02:43,000 --> 00:02:47,714 So we know that the function is expected to return the cost per person. 50 00:02:47,714 --> 00:02:52,562 But in order to make sure that things are clear here I'm gonna make a completely 51 00:02:52,562 --> 00:02:54,330 different named variable. 52 00:02:54,330 --> 00:03:00,195 Let's call it, let's call that amount_due. 53 00:03:00,195 --> 00:03:01,976 I don't wanna confuse anybody. 54 00:03:01,976 --> 00:03:04,564 So, again, you call a function, 55 00:03:04,564 --> 00:03:09,620 we're gonna assign amount_due to the result of this function. 56 00:03:09,620 --> 00:03:13,920 So, you call the function, so we'll say split_check, and 57 00:03:13,920 --> 00:03:16,270 again you call it with parens. 58 00:03:16,270 --> 00:03:20,007 And you put the arguments in here for the defined parameters. 59 00:03:20,007 --> 00:03:25,939 So for total, say that was $84.97. 60 00:03:25,939 --> 00:03:30,087 And then for the second parameter, that line's up, it was number_of_people, so 61 00:03:30,087 --> 00:03:30,786 we'll do 4. 62 00:03:30,786 --> 00:03:35,279 There were 4 people, it cost $84.97. 63 00:03:35,279 --> 00:03:42,140 So now the result from split_check is stored in amount_due. 64 00:03:42,140 --> 00:03:48,435 Just like we saw in our notifications here when we did this call to len(text). 65 00:03:48,435 --> 00:03:51,034 So len returned the number_of_characters and 66 00:03:51,034 --> 00:03:54,273 we stored it in a variable called number_of_characters. 67 00:03:54,273 --> 00:03:57,821 The same thing here we are calling split_check and 68 00:03:57,821 --> 00:04:00,307 we're storing it in amount_due. 69 00:04:00,307 --> 00:04:03,170 And remember when we call this, this 84.97 gets set. 70 00:04:03,170 --> 00:04:07,668 We can basically say, total equals 84.97 and then run this code and 71 00:04:07,668 --> 00:04:09,410 then same thing here. 72 00:04:09,410 --> 00:04:12,902 Just imagine this being number_of_people equals 4, and 73 00:04:12,902 --> 00:04:16,605 then when it's in here it's 4, and this total is 84.97. 74 00:04:16,605 --> 00:04:20,040 And our function returned the value cost_per_person, 75 00:04:20,040 --> 00:04:22,520 which we stored in amount_due. 76 00:04:22,520 --> 00:04:26,116 So since we now have a value, why don't we print it out? 77 00:04:26,116 --> 00:04:31,348 So we'll say, print("Each person owes ${}" and 78 00:04:31,348 --> 00:04:38,157 then we'll go ahead and format that, and we'll say (amount_due). 79 00:04:39,233 --> 00:04:42,793 So let's run it, python check_please. 80 00:04:45,483 --> 00:04:51,872 It worked but yikes, right, this is why you don't wanna use floats for currency. 81 00:04:51,872 --> 00:04:55,679 Now see these additional fractions of a cent over here? 82 00:04:55,679 --> 00:05:01,745 I'm pretty sure most people don't have that extra $0.0025 on them. 83 00:05:01,745 --> 00:05:06,530 In fact I'm pretty sure that most people don't even have the $0.24 on them. 84 00:05:06,530 --> 00:05:08,440 So what happens usually, 85 00:05:08,440 --> 00:05:12,130 is whoever is paying the bill ends up paying the extra money for everybody. 86 00:05:13,380 --> 00:05:15,990 We should fix that in our version of this app. 87 00:05:15,990 --> 00:05:20,630 The fairest solution that I can think of is to round up to the next dollar. 88 00:05:20,630 --> 00:05:25,890 Now note, if we use the round function here it would round down, 89 00:05:25,890 --> 00:05:28,480 right, cuz $21.24 will go down to $21. 90 00:05:28,480 --> 00:05:32,450 But I'm suggesting that we should pay $22. 91 00:05:32,450 --> 00:05:36,280 As you can imagine, there is a way to do this mathematically. 92 00:05:36,280 --> 00:05:39,380 We could write it ourselves, but you know what? 93 00:05:39,380 --> 00:05:42,030 There's a function that exists for us already. 94 00:05:42,030 --> 00:05:45,730 The thing is though, we need to tell our script that we want to use it, 95 00:05:45,730 --> 00:05:47,370 it's not here by default. 96 00:05:47,370 --> 00:05:51,538 In order to bring in this grouping of new tools, we need to use a new keyword, 97 00:05:51,538 --> 00:05:52,918 that keyword is import. 98 00:05:52,918 --> 00:05:56,617 Here let's take a look at it in the REPL real quick. 99 00:06:01,982 --> 00:06:08,570 Now the name of the module or grouping of tools that I want to use is called math. 100 00:06:08,570 --> 00:06:11,750 We'll do a deeper look at modules and how to create them in a future course. 101 00:06:11,750 --> 00:06:16,420 But for now, we want to get our hands on some math functions. 102 00:06:16,420 --> 00:06:21,248 Okay so we're gonna import, the name of the module is math. 103 00:06:21,248 --> 00:06:27,604 And we can use functions in a module by writing the module name, so math. 104 00:06:27,604 --> 00:06:30,640 And then we use dot notation to get to the function, so dot. 105 00:06:30,640 --> 00:06:34,005 And the name of the function is ceil, 106 00:06:34,005 --> 00:06:40,790 C-E-I-L, not the singer, which is short for ceiling. 107 00:06:40,790 --> 00:06:44,450 Basically what it does is always rounds up to the next closest integer, 108 00:06:44,450 --> 00:06:45,120 so let's do that. 109 00:06:45,120 --> 00:06:52,040 So we'll do 21.24 is what we had right, so this should round up to 22, and it does. 110 00:06:52,040 --> 00:06:56,130 Now, of course, if you're interested, you can take a look at all other functions 111 00:06:56,130 --> 00:07:01,010 in the math module by doing help(math). 112 00:07:01,010 --> 00:07:05,580 But again, don't feel like you need to understand all of these functions yet. 113 00:07:05,580 --> 00:07:09,800 I'm just showing you that you have a ton of functions here for you just waiting for 114 00:07:09,800 --> 00:07:12,870 you to call them, and this is just one module. 115 00:07:12,870 --> 00:07:16,020 Feel free to explore them, you aren't gonna break anything. 116 00:07:16,020 --> 00:07:16,800 More in teacher's notes. 117 00:07:16,800 --> 00:07:20,356 I'm gonna drop out of the help, I'm gonna drop out of the shell here too, 118 00:07:20,356 --> 00:07:24,700 then I'm gonna clear. 119 00:07:24,700 --> 00:07:30,630 So let's use that math module in that ceiling function in our file. 120 00:07:30,630 --> 00:07:35,540 So style suggests that we should put all imports at the top of our file. 121 00:07:35,540 --> 00:07:40,000 So I'm gonna do that and we'll say import math. 122 00:07:40,000 --> 00:07:43,190 And now that math has imported we can just it when we need it. 123 00:07:43,190 --> 00:07:46,550 And so what we want here is we want the ceiling of this value, right? 124 00:07:46,550 --> 00:07:49,980 So we could say, math.ceil and there we go. 125 00:07:49,980 --> 00:07:54,370 So what happens is this will run and we'll get that 21.2455 whatever and 126 00:07:54,370 --> 00:07:59,080 this will do the ceiling, it will round it up to the nearest integer. 127 00:07:59,080 --> 00:08:00,696 So let's go ahead, let's re-run that. 128 00:08:03,333 --> 00:08:08,100 And boom, $22, nobody's getting ripped off anymore, amazing. 129 00:08:08,100 --> 00:08:11,330 So, I suppose the next step to this is to allow for 130 00:08:11,330 --> 00:08:15,030 our script to take dynamic input from our users. 131 00:08:15,030 --> 00:08:16,760 So, let's see, let's just do that. 132 00:08:16,760 --> 00:08:22,863 We'll go here and we'll say the total_due = ("what 133 00:08:22,863 --> 00:08:27,210 is the total?"), give it some space. 134 00:08:28,320 --> 00:08:31,640 Now that's gonna return a string, remember, but we want it to be a float. 135 00:08:31,640 --> 00:08:36,957 So let's coerce that, so we'll just wrap this, we'll say float, awesome. 136 00:08:36,957 --> 00:08:41,067 And next we need the number of people, so let's coerce that to an int. 137 00:08:41,067 --> 00:08:45,378 So we'll say number_of_people = 138 00:08:45,378 --> 00:08:50,528 int(input( "How many people?")). 139 00:08:52,747 --> 00:08:55,860 See how the braces line up there, awesome. 140 00:08:55,860 --> 00:09:00,190 And now that we have those values, we'll use them in our function call here. 141 00:09:00,190 --> 00:09:04,823 So we'll say, let's just move this, we'll get rid of this here, move this down here. 142 00:09:04,823 --> 00:09:07,767 Say the amount_due is, so the total_due, 143 00:09:07,767 --> 00:09:12,349 we'll pass that in there and we'll pass in the number_of_people. 144 00:09:15,977 --> 00:09:16,589 Here we go. 145 00:09:18,639 --> 00:09:21,760 Let's run that real quick, let's make sure that works. 146 00:09:21,760 --> 00:09:25,500 What's the total, we got a 100 divided by 3 people, $34, awesome. 147 00:09:27,330 --> 00:09:29,050 One more little bit of clean up here, 148 00:09:29,050 --> 00:09:31,610 I wanna point out something that I did intentionally. 149 00:09:31,610 --> 00:09:34,160 Now see how in the function body here, 150 00:09:34,160 --> 00:09:36,910 we are creating a variable called cost_per_person. 151 00:09:36,910 --> 00:09:40,890 And we never actually use it, we just return it. 152 00:09:40,890 --> 00:09:45,160 Well we can, and we should, skip that unnecessary variable assignment. 153 00:09:45,160 --> 00:09:46,700 It's a little strange to see it first, 154 00:09:46,700 --> 00:09:49,660 which is why I used the variable assignment originally. 155 00:09:49,660 --> 00:09:54,958 So, we know that math.ceil is a function that returns an integer, right? 156 00:09:54,958 --> 00:09:59,850 So, I'm gonna take this, I'm gonna cut this, and we'll paste this right here. 157 00:09:59,850 --> 00:10:02,893 What we can do is we can just return the result. 158 00:10:05,924 --> 00:10:12,790 So you can read this like math.ceil returns an integer value. 159 00:10:12,790 --> 00:10:17,834 And then we return that value to the caller of split_check. 160 00:10:19,220 --> 00:10:20,160 I hope that makes sense, 161 00:10:20,160 --> 00:10:23,610 there's no need to create that variable just to turn around and return it. 162 00:10:23,610 --> 00:10:28,360 So now we've got a super functional function, right? 163 00:10:28,360 --> 00:10:29,460 What could go wrong? 164 00:10:29,460 --> 00:10:32,550 Well, as soon as you bring users into the equation, 165 00:10:32,550 --> 00:10:35,390 you'll start to see errors that you didn't anticipate. 166 00:10:35,390 --> 00:10:38,390 Like, let's go ahead and run this again. 167 00:10:38,390 --> 00:10:43,161 Let's say that the bill total was 20 and 168 00:10:43,161 --> 00:10:47,667 then something like this, four of us. 169 00:10:47,667 --> 00:10:49,910 How many people, there were four of us. 170 00:10:49,910 --> 00:10:52,520 Yuck, we should probably handle that better. 171 00:10:52,520 --> 00:10:56,048 That error is pretty intense for a user, isn't it? 172 00:10:56,048 --> 00:10:57,875 Watch this classic one. 173 00:10:57,875 --> 00:11:03,334 We're gonna call the check_please.py, and it was $40 and 174 00:11:03,334 --> 00:11:09,714 I'm gonna put a 0 saying 0 people are paying ZeroDivisionError, no! 175 00:11:09,714 --> 00:11:12,762 If you don't remember from your math class, you can't divide by 0. 176 00:11:12,762 --> 00:11:16,728 It like opens a wormhole in outer space or something intense like that. 177 00:11:16,728 --> 00:11:20,520 We don't want our users doing that, we don't want them seeing this. 178 00:11:20,520 --> 00:11:23,480 So let's take a quick break and then swing back into our script and 179 00:11:23,480 --> 00:11:26,360 protect our users from themselves.