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!

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

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

Make sure that, using your new program, Reeborg can still add 3+2.

- Using your program, can Reeborg add 8+4 as follows?.

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.

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 10^{th} 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 1^{st} avenue and 1^{st} street).

Have Reeborg do the following:

- Put a line of beepers across 10
^{th}street as you have done before. - Go to the bottom right of the screen, collecting the two piles (8 and 4) of beepers.
- 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(): ...`)

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

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

- 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. - Have Reeborg carry these beepers down, until Reeborg reaches the wall;
- Have Reeborg put them all down (3), pick one up (the carry over), move west and put the carry over beeper down.

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.