yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Templating a contract with variables | Intro to CS - Python | Khan Academy


4m read
·Nov 10, 2024

Let's work together on a program that uses variables and user input. Here's the problem I'm trying to solve: my friend Deshawn has a catering business, and for each catering job that he takes, he needs to write up a contract between him and the client. Every contract uses the same basic template, but there are blanks that need to be filled in with the event details, like the client name, the venue name, and the date.

These contracts end up being a lot of pages of legal speak, and a lot of these event detail values need to be copied onto multiple pages. Can we build Deshawn a program to help him generate contracts faster? Here we have a bit of code to get us started. Deshawn copied over the first paragraph of his contract template for us to work with.

The first thing I do when I get starter code like this is run it and see if it works and what it does. It looks like it formats the contract a bit with this nice heading, and then it just kind of prints out the template as is. There's still like these blanks here that need to be filled in. Of course, Deshawn's full contract is a lot longer than this, but I kind of like the idea of starting with just this first paragraph to get a feel for how I'm going to solve this problem with a smaller sub-problem before I copy in a ton of text.

One thing I do notice with this code is that we have a linter error over here. It's one of these yellow triangles, so that means it's a warning, not an error, but I still want to take a look at it and see if it's something I can fix. It looks like it's complaining about this line being too long, which, you know, I kind of agree with. I have to horizontal scroll all the way over here just to get to the end of the string. It makes it hard to work with.

To split a long string like this over multiple lines, I can divide the string into several smaller strings and then concatenate them together. Then, if I surround that whole expression in parentheses, I can put these strings on separate lines. Because of the parentheses, the computer treats this whole thing as a single instruction. Before we move on, I'm just going to quickly rerun the program and make sure I didn't break anything while I was here.

Okay, now let's think through how to design this program. I want to generalize this code so I can substitute in the name of any client. The user experience that I'm going for is that Deshawn can come in and enter all the event details, essentially in text boxes, and then the program will give him the completed contract.

For this first paragraph, it looks like the values I'm trying to fill in are the name of the client and the name of the catering company. So, I probably want a variable for each of those. I'm going to want Deshawn to be able to enter some of those values, so I'll probably need an input function call here. But I don't want to have to enter values in the console every time I run the program while I'm still working on it, so I'm just going to stick some placeholder values in here for now.

All I did was add a couple assignment statements that I think are right, so I don't think there should be any bugs. But just to be sure, I'm going to run the program. Alright, now I want to substitute those values in where the blanks are. That means I need to access these variables in this expression so I can substitute the value stored there into the contract string.

So, where I have these underlines in the string, I'm going to delete that blank and then separate the string into two parts at that juncture. There's a lot of strings, so let me check I didn't miss any quotation marks. Great, okay now let's substitute my values in here. I want to concatenate in the caterer name, which is stored in the variable caterer, and over here I want to concatenate in the client name, which is stored in the variable client.

Now when I run the program, I'm seeing those placeholder values I stored in those variables in place of the blanks in the contract. This messed up my line lengths over here a bit, so I'm going to clean that up real quick so it's easier to read. There's one more blank here in the header, and it sounds like Deshawn wants the catering company name to go here, so I'm going to do the same thing and divide that string at the blank and substitute in the caterer variable.

Last step, let's go back to these placeholder values. Deshawn's company is called King Catering, so I'm going to put that in here. The client name, though, is different on every contract, so I want Deshawn to be able to enter that value. That means I'm going to want the input function here, and I'm going to make sure I include a descriptive prompt so Deshawn knows what value to enter.

To test that it works, I'm going to run the program and imagine I'm Deshawn trying to generate a new contract for a client. I enter in the client's name, and then it spits out the first paragraph of my contract. Woo! I have a prototype working. I'm going to take this back to Deshawn and see what he thinks. Once I get his feedback, I can come back in and follow this pattern to generate the rest of the contract, or maybe you want to give it a try.

More Articles

View All
The actual reason why you procrastinate and how to fix it
It’s 6:00 p.m. You just got back home, and you’ve got a task that has been lingering in your mind, waiting to be checked off your list. It could be a project for work or school, house chores that can’t be ignored any longer, or maybe it’s about spending q…
Why Rich People Are Cheap
It’s a cotton stereotype self-perpetuated throughout history: rich people are cheap. We’ve seen this demonstrated and exaggerated in everything from fictional characters like Mr. Burns from The Simpsons and Ebenezer Scrooge from A Christmas Carol, all the…
Hyperinflation Explained: The 100 TRILLION Dollar Banknote
I’ve got something cool to show you guys today. Something that came in the mail just the other day. Most people wouldn’t get excited about this, but I’m actually pretty excited about it. I’ll show you a close-up. This is it! That right there is a 100 tril…
Brexit and European Union primer
Given all of the recent talk about the United Kingdom deciding to leave the European Union, often referred to as Brexit (short for British exit from the European Union), I thought it would be interesting to do a primer on, well, what exactly is the Europe…
America Inside Out with Katie Couric - First Look | National Geographic
KATIE COURIC (VOICEOVER): Is shifting before our eyes. Race you to the top, Mike. (VOICEOVER) Big changes– Hi, Henry. HENRY: Hi, Katie. KATIE COURIC (VOICEOVER): –big challenges– I hate to admit it, but I probably am prejudiced. KATIE COURIC (VOICEOV…
Quadratic systems: a line and a parabola | Equations | Algebra 2 | Khan Academy
We’re told the parabola given by ( y = 3x^2 - 6x + 1 ) and the line given by ( y - x + 1 = 0 ) are graphed. So you can see the parabola here in red and we can see the line here in blue. The first thing they ask us is, one intersection point is clearly id…