Teaching Reeborg to add

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!


1. Warmup

  1. Write a program that has Reeborg adding 3+2 in the following way.

    3+2lead to 5


  2. Change your program so that Reeborg can 13+22 in the following way, where each beeper pile represents a digit.

    13+22lead to 35


    Make sure that, using your new program, Reeborg can still add 3+2.
  3. Using your program, can Reeborg add 8+4 as follows?.

    8+4lead to 12


    Can you think of a way to change it so that it does? You may want to try on your own before you read further.

2. Addition review

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:

adding startlead to adding end

Let's tackle first the simpler problem of adding 8+4.


3. Adding 8+4 in base 10.

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.

line of beepers accross 10th street

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).

8+4

Have Reeborg do the following:

  1. Put a line of beepers across 10th street as you have done before.
  2. Go to the bottom right of the screen, collecting the two piles (8 and 4) of beepers.
  3. Spread those 12 beepers on a vertical column as illustrated below.
    (Important: you will need to use the test
    carries_beepers() which I haven't told you about ... yet! Try something like
    while carries_beepers(): ...)

8+4 spread vertically

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

  1. Have Reeborg pick up the 9 beepers below the horizontal line, and discard them (perhaps by putting them all on the horizontal line);
  2. Have Reeborg keep going north past the last beeper, as illustrated below:

    8+4

  3. Have Reeborg turn around, pick up one beeper and move, repeat until Reeborg reaches a corner where there is no more beeper to pick up (below the horizontal line); at this point, Reeborg should be carrying three beepers.
  4. Have Reeborg carry these beepers down, until Reeborg reaches the wall;
  5. Have Reeborg put them all down (3), pick one up (the carry over), move west and put the carry over beeper down.
All that is left to do is to move Reeborg out of the way to display the result!

8+4

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!


4 Adding 3+5

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!


5 Adding multi-digit numbers.

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.


6. Final challenge

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.


7. What next?

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.

previous Avoiding repetitions - the important stuff - home - Python already knows how to add.next
../images/SourceForge.net Logo