In this lesson, I will guide you in writing a program that will have Reeborg add two numbers. While we will do this in the usual way, using numbers written in base 10, we will be able to easily use the program to add numbers in other bases!
Let us add two numbers in the "traditional way", from left to right:
528 + 634 ------ 12 # adding the units first (8+4)
We note that we have a "1" to carry over the "tens" column. This "carry over" is most likely where your program had problems. Let us rewrite it in the "usual" way and continue.
1 528 + 634 ------ 1162
Ok, that was a bit brief, but I'm sure you were able to follow. In Reeborg's world, we would like this addition to look as follows:
Let's tackle first the simpler problem of adding 8+4.
As we have mentioned, the problem of adding numbers so that each beeper pile represents a single digit comes when we have to add two numbers whose sum is greater than 9 (in base 10). Somehow, we need to keep track of this magic number (10), no matter what two numbers we are going to add. I have created a world (file: adding_world.wld) that is big enough to add two 7-digit numbers in base 10 (or even in base 29!). Load up that world file and I will guide you so that you can write a program that can do additions properly.
After loading the world file, if you look at the bottom of the screen, you will see that Reeborg carries 8 beepers. Write a program so that Reeborg puts a line of beepers, one at each corner of 10th street, as illustrated below (after my program ended, I used the cursor keys to move Reeborg out of the way as he was standing on top of the beeper in the last column.
Now, make sure you save this program before going any further.
Reload the world file and add beepers in the bottom right corners so that the display looks like the following, but with Reeborg standing at the origin (corner of 1st avenue and 1st street).
Have Reeborg do the following:
Now, we have two beepers above the horizontal line of beepers (the units in the number 12) and an extra beeper on the horizontal line (which we can use as the "carry over"). So, all that we you have to do is
Well ... actually, those five steps as I wrote them will require writing a fair bit of code and you might find it a bit difficult to get it right. But you will if you proceed systematically. Try it out!
So, you finally got your program to calculate 8+4. Great! Now try it on 3+5. Does it work? Chances are, it doesn't ... as this doesn't require a carry over. Go back and fix it so that all simple two-digit additions from 0+0 to 9+9 work properly!
The next thing to do is to generalize this from single-digit additions to multi-digit ones. My suggestion is to "wrap" the main part of the code inside a while loop, over all the columns. This is left as an exercise.
If you know what it means to add numbers in bases other than base 10, try to modify your program so that you can add numbers in a different base.
So far, within Reeborg's world, we have seen the following Python keywords:
def, elif, else, if, not, pass, while. We have ended on writing a rather complicated program so that Reeborg could add two numbers. It is time to leave Reeborg's world to see how we can add two numbers much more easily within Python. We will learn quite a bit more about Python before we return to Reeborg's world.