Programming 2 with Michael Gallo

by / January 18, 2014 Technology No Comments

Post By RelatedRelated Post


Now the fun can begin. Before break, this column introduced the basics of computer programming — what programming is, how to set your computer up for it, and what a basic program looks like. It was good background. But it didn’t let the computer flex its muscles. This week that changes with the if-then structure.

Computer circuits are packed with electronic “logic gates”. It might make sense then that they are good at making logical decisions. In fact, this might represent the very essence of programming. All questions of what computers can and cannot do ultimately boil down to questions of an inherent logical structure to the task. Is it possible to make a computer carry on a conversation like a human? What that question is really asking is if people follow a set of rules to determine when to say hi, when to crack a joke, or when to offer a compliment. Find those rules (if they exist), and you’ve got yourself a friend.

Making a computer talk is tough. Let’s instead do something simpler: let’s see if we’re over budget.

The building block here is the if-then loop, and it works as one might expect. The computer tests IF something is true, and if it is THEN it performs the desired step.

Here’s what it looks like in the python language:

if [condition to be tested]:
[thing to do]

Python is actually a little quirky in that you don’t actually write “then”. A lot of older programming languages require the “then” be written explicit, but python realised it’s basically redundant.

The IF structure makes use of logical tools. “Greater than”, “less than”, “and”, “or”, “equal to” are mostly fair game. Let’s apply these to our budget problem. We’re over budget when the amount of money we’re actually spending is more than the amount we intended to. Or, in python parlance:

if spent > budget

We’ll just print “Over budget” if we go over budget.

if spent > budget:
print “Over budget”

That’s pretty much the crux of it. But now we need to actually fill in some details, like what our budget actually is and how much we’ve actually spent. Let’s have the user tell their budget. They they’ll tell the program how much they’ve spent, and the program will tell them whether they’re over budget.

In python you get user input with the “input([prompt])” command. Let’s set the user’s response equal to a variable called “budget”.

budget = input(“What is your budget? “)

Now let’s find out how much they’ve spent.

spent = input(“How much have you spent? “)

And putting it all together:

budget = input(“What is your budget? “)
spent = input(“How much have you spent? “)

if spent > budget:
print “Over budget”

There it is. There are many problems certainly (what if the user types a letter instead of a number?), but one is particularly glaring from a user interaction standpoint. If the person is under budget, you’ll notice nothing actually happens. The user might be able to assume that no news is good news, but there’s a relatively easy fix to avoid requiring such a bold assumption. Let’s include an “else:” statement. The computer will execute that statement when the if statement is not true.

if spent > budget:
print “Over budget”
print “Within budget”

Now the user knows that the program is actually working, even if he’s under budget. Now let your understanding of conversations inform your new knowledge of if-then statements and go make some headlines.