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 right to left:
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.
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 digits 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:
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 you have to do is
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!
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.
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.
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.