Neko goes in a loop

Cricket and Homework

Anjali was not the kind of girl who would do homework
when her Dad was enjoying the Indo-Pak cricket match
on TV. Her class teacher however had no mercy for girls
who watched cricket instead of doing their homework - and
that’s how she ended up writing that English poem
in her notebook a hundred times the next day.

Poor Anjali - she would have been bored to death writing
that stupid imposition! That’s the case with all of us -
we hate doing dull, repetitive work.

But our friend Python is quite different - she has the ability to
do any amount of repititive work without
getting bored! And that is something which is very useful for us!

Neko goes in a loop

We know how to make Neko run left, right, up and down. The
trouble is, if we wish to make Neko run say five places to
the right, we will have to type the function `right’ five
times. Imagine a big chess board with 100 rows and 100 columns -
typing `right’ sixty times so as to move Neko sixty places
to the right is simply stupid, is it not?

But remember, it’s very easy to ask Python to do something
again and again and again …. a thousand, no, a million times!
And, she doesn’t get bored!

How do we do this?

Simple. Just write a letter!


Dear Python,
I want you to move Neko 8 places to the right. Here is how you
can do this.

First, create a name tag (variable) called `count’ and give it the
value zero. As long as the value of `count’ is less than eight, repeat the
following steps:

a) Move right
b) Increase the value of `count’ by one

We know how to create a variable called `count’:

count = 0

We know how to move right:

right()

We know how to increase the value of `count’ by one:

count = count + 1

What we don’t know is how to say:

“Repeat the following steps as long as count is less than eight”

The way you say this in Python is:

while count < 8

Now, we can put all these things together and write a letter which Python
will understand:

count = 1
while count < 8:
    right()
    count = count + 1

That’s short and sweet!

This is what you will see at the Python prompt:

>>> count = 1
>>> while count < 8 :
...    right()
...    count = count + 1
...
>>>

Take care to see that you type the `colon’ after `count < 8′. When you
hit “enter”, Python will display three dots; this is to tell you that
whatever you type next will be repeated as long as `count’ is less than
eight. You have to type a `tab’ (remember, we did the same thing when
we defined functions) and then type `right()’. After you have typed all
the lines which you wish to get repeated, type a blank line and hit
“enter”. You will get back the Python prompt!

What is the effect on Neko? You will see him running to the right!

What does Python actually do when she reads this `letter’?

She creates a variable called `count’ and sets it to 0.

She asks: is `count less than 8′? Yes (`count’ is at present 0)!

So she does a `right’ and after that, does `count = count + 1′. This
will set `count’ to 1.

She comes back again to the top and asks: `is count less than 8′? Yes (`count’
is at present 1)!

She again does a `right’ and after that, `count = count + 1′. This will
set `count’ to 2.

She comes back again to the top and asks: `is count less than 8′? Yes (`count’
is at present 2) !

And so on ….

At one point, `count’ becomes 7. Python asks the question: `is count less than
8′. Yes! So she does a right() and also does `count = count + 1′. This makes
`count’ equal to 8.

Now, when Python asks the question: `is count less than 8′, the answer is NO!

Remember, `while count < 8′ means: `repeat the following as long as
count is less than 8′; `count’ is no longer less than 8. So Python
will simply stop the process of repitition.

Programmers call this a loop!

They will also tell you that the loop has `terminated’ because the
`condition’ `count less than eight’ has become false!

Things to do

Here are some things to do on your own.

Write a loop which will make Neko run down eight cells.

Predict how many places Neko will move in the following case:

count = 6
while count < 9 :
    right()
    count = count + 1

Place Neko at the top left cell. Then write a loop which will
make him move as if he is going down a staircase (that is, Neko
goes down, then right, down, right … until he reaches the
last row and last column).

Post a Comment

Your email is never published nor shared.