Neko moves to the rightmost cell - part 1

Neko moves to the rightmost cell - Part 1

How do you make Neko move to the rightmost cell? Just type:

i = 0
while i < 8:
	right()
	i = i + 1

Ok. This will work if Neko is in the leftmost cell initially.

What if Neko is at the centre? What if your chessboard has much more
than 8 columns? Is it possible for you to write a loop which will
always take Neko to the rightmost cell whatever be the current
position and the number of columns in the board?

Think a bit … make sure that you understand the problem clearly.

There is a way to do this - but you will have to understand a bit more
about how Python functions work.

Functions and return values

Move Neko to the top left corner and then type `right()’ at the
Python prompt. This is what you will see:

>>> right()
1
>>>

Let’s now try an experiment:

>>> a = right()
>>> a
1
>>>

What did you observe?

The number that the function `right()’ prints onto the screen is
now stored in `a’. That’s all! You have seen that value of `a’
is indeed 1.

It is as if you are `capturing’ the value `returned’ by the
function and storing it in a variable!

You can stop reading this chapter now … and come back to
the remaining part later. Or, if you like to have some more
fun, read on!

Let’s try one more experiment. Type the following code:

def simpletest():
	print 1+2

Now, run it:

>>> simpletest()
3
>>> a = simpletest()
3
>>> a
>>>

Now, that’s strange! We are not able to `capture’ the value which
the function displays on the screen! We have seen that nothing is
stored in `a’!

One more experiment.

>>> def anothertest():
...    return 1 + 2
...
>>> a = anothertest()
>>> a
3
>>>

The idea is very simple. If you want to capture a value from a
function, say the value 134, you have to write `return 134′.
Simple printing will not do.

If all these things seem confusing - don’t worry. Just remember
one point - you can store the number which the function `right’
(and also functions up, down, left) prints on the screen by
simply typing:

a = right()

This might seem silly - but in the next chapter, we will use
this idea to do something interesting!