## 24. 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!

### Warmup

1. Write a program that has Reeborg adding 3+2 in the following way.
   Write it so that it would also work to add 1+4.
2. Change your program so that Reeborg can 13+22 in the following way, where each beeper pile represents a digit.
3. Can you write a program so that Reeborg can still add 3+2 but also so that it can add 8+4 as follows?.

   Your new program should also add correctly 2+5 and 9+9. Can you see how to do this? If so, try ... However, if you can not, do not get discouraged; instead, read on...

### 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:

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

### Adding 8+4 in base 10

Warning: The following exercise may be a bit difficult and is presented more as a challenge. Read the rest of this lesson and decide for yourself if you want to try it or proceed directly to learning some more about Python.

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 16!). 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; this is why you don't see him in the picture below.)

Now, make sure you save this program before going any further.
Reload the world file (so that the world is empty again and Reeborg is standing at the origin [corner of 1st avenue and 1st street]) and add beepers in the bottom right corners so that the display looks like the following (but with Reeborg at the origin).

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.

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 you have to do is

1. Have Reeborg pick up the 9 beepers below the horizontal line of beepers, and discard them (perhaps by putting them all on the horizontal line); you can do this by repeating the following steps:
   1. pick up a beeper
   2. check to see if there is another beeper to pick up; if not
   3. move ... until you reach a corner where there is another beeper to pick up ...
   at which point you will have reached the horizontal line of beepers. You can drop them all at that corner.
2. Have Reeborg keep going north at least past the last beeper, as illustrated below:

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!

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!

### 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. Can you think of a way to do it? Perhaps you can using only what we have learn so far... For the solution I wrote, I needed to use a Python keyword we haven't seen yet: break.

### Final challenge

If you know what it means to add numbers in bases other than base 10, try to modify your program (or the world file?...) so that you can add numbers in a different base.

### 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 with "pure" Python. Don't worry: we will come back to Reeborg's world from time to time, and see how our increased knowledge of Python can help Reeborg.

After the storm - - Python already knows how to add.