1 00:00:07,860 --> 00:00:12,235 The last thing I want to talk about in this lesson is hard coding, 2 00:00:12,235 --> 00:00:17,140 and before I get to that I'm just going to tell a motivating story from my childhood. 3 00:00:17,140 --> 00:00:22,560 So, I have three older siblings and when they were doing algebra, 4 00:00:22,560 --> 00:00:24,645 if you remember algebra, 5 00:00:24,645 --> 00:00:27,480 then you often get problems of the type: solve for x, 6 00:00:27,480 --> 00:00:29,550 solve for y, solve for z. 7 00:00:29,550 --> 00:00:32,110 One day, I overheard my sister talking on 8 00:00:32,110 --> 00:00:35,195 the phone with a friend about a math problem that they had, 9 00:00:35,195 --> 00:00:40,635 and they were trying to solve for the value of x and I overheard my sister saying, 10 00:00:40,635 --> 00:00:42,420 "The value of x is three." 11 00:00:42,420 --> 00:00:43,990 When I heard that, 12 00:00:43,990 --> 00:00:48,760 I thought that I had found out some hidden secret about algebra. X is three. 13 00:00:48,760 --> 00:00:52,160 I could take every single problem that involves x and just 14 00:00:52,160 --> 00:00:56,510 substitute three and I'd crack the code and I didn't have to learn algebra anymore. 15 00:00:56,510 --> 00:00:58,195 I could just skip that grade. 16 00:00:58,195 --> 00:01:00,615 Of course, if you've actually taken algebra, 17 00:01:00,615 --> 00:01:02,715 then you know that x isn't three. 18 00:01:02,715 --> 00:01:06,315 You know that you have to solve for the value of x every single time. 19 00:01:06,315 --> 00:01:10,585 So, what I was thinking of doing there was a form of hard-coding, 20 00:01:10,585 --> 00:01:14,650 or thinking that x is three for every single problem. 21 00:01:14,650 --> 00:01:19,505 So, in the context of solving Python programs, what hard-coding is, 22 00:01:19,505 --> 00:01:21,740 is it's writing down the answer without 23 00:01:21,740 --> 00:01:24,830 actually going through the work of computing the answer. 24 00:01:24,830 --> 00:01:27,440 So, let's suppose that I want to write 25 00:01:27,440 --> 00:01:31,085 a program to print out the value of x and y averaged. 26 00:01:31,085 --> 00:01:36,670 So, if I have x equal to 10 and y equal to 20, 27 00:01:36,670 --> 00:01:42,705 then I could hard-code my answer by saying average equals 15.0, 28 00:01:42,705 --> 00:01:44,400 and I, let's say, 29 00:01:44,400 --> 00:01:48,825 print out that average, 30 00:01:48,825 --> 00:01:52,940 and so yes, in this case I happened to get the right answer, 31 00:01:52,940 --> 00:01:56,060 that the average of x and y is 15, 32 00:01:56,060 --> 00:02:00,115 but my answer is hard-coded because I said that the average was 15, 33 00:02:00,115 --> 00:02:04,850 and what that means is that if I change the value of x here to be 30, 34 00:02:04,850 --> 00:02:07,550 the average should be 25.0, 35 00:02:07,550 --> 00:02:11,650 but instead I still get 15 because my answer was hard-coded. 36 00:02:11,650 --> 00:02:13,605 So, what I'll do is, 37 00:02:13,605 --> 00:02:17,035 instead of writing out average equals 15, 38 00:02:17,035 --> 00:02:19,460 I want to actually write out the expression that's going 39 00:02:19,460 --> 00:02:22,425 to reference x and y to compute that average. 40 00:02:22,425 --> 00:02:29,815 So, I'll say average equals x plus y divided by two, and so, 41 00:02:29,815 --> 00:02:33,020 even though I still get the same answer of 15, 42 00:02:33,020 --> 00:02:36,050 then because this answer is no longer hard-coded, 43 00:02:36,050 --> 00:02:38,770 if I change the value of x to 30, 44 00:02:38,770 --> 00:02:41,010 then my answer is still going to be correct. 45 00:02:41,010 --> 00:02:45,640 Same thing if I change the value of my answer, y, to 40. 46 00:02:45,640 --> 00:02:47,880 So, in your assignments, 47 00:02:47,880 --> 00:02:50,760 we'll often say, "Don't hard-code your answer." 48 00:02:50,760 --> 00:02:54,830 What that means is that you have to write expressions that actually reference 49 00:02:54,830 --> 00:02:59,000 the variables and rather than just jumping straight to the answer, 50 00:02:59,000 --> 00:03:02,210 you actually have to write expressions and statements that will actually 51 00:03:02,210 --> 00:03:06,670 compute the answer in your code. Until next time.