The last thing I want to talk about in this lesson is hard coding,
and before I get to that I'm just going to tell a motivating story from my childhood.
So, I have three older siblings and when they were doing algebra,
if you remember algebra,
then you often get problems of the type: solve for x,
solve for y, solve for z.
One day, I overheard my sister talking on
the phone with a friend about a math problem that they had,
and they were trying to solve for the value of x and I overheard my sister saying,
"The value of x is three."
When I heard that,
I thought that I had found out some hidden secret about algebra. X is three.
I could take every single problem that involves x and just
substitute three and I'd crack the code and I didn't have to learn algebra anymore.
I could just skip that grade.
Of course, if you've actually taken algebra,
then you know that x isn't three.
You know that you have to solve for the value of x every single time.
So, what I was thinking of doing there was a form of hard-coding,
or thinking that x is three for every single problem.
So, in the context of solving Python programs, what hard-coding is,
is it's writing down the answer without
actually going through the work of computing the answer.
So, let's suppose that I want to write
a program to print out the value of x and y averaged.
So, if I have x equal to 10 and y equal to 20,
then I could hard-code my answer by saying average equals 15.0,
and I, let's say,
print out that average,
and so yes, in this case I happened to get the right answer,
that the average of x and y is 15,
but my answer is hard-coded because I said that the average was 15,
and what that means is that if I change the value of x here to be 30,
the average should be 25.0,
but instead I still get 15 because my answer was hard-coded.
So, what I'll do is,
instead of writing out average equals 15,
I want to actually write out the expression that's going
to reference x and y to compute that average.
So, I'll say average equals x plus y divided by two, and so,
even though I still get the same answer of 15,
then because this answer is no longer hard-coded,
if I change the value of x to 30,
then my answer is still going to be correct.
Same thing if I change the value of my answer, y, to 40.
So, in your assignments,
we'll often say, "Don't hard-code your answer."
What that means is that you have to write expressions that actually reference
the variables and rather than just jumping straight to the answer,
you actually have to write expressions and statements that will actually
compute the answer in your code. Until next time.