LightReader

Chapter 15 - Chapter 15 — The Repeating Machine

The cursor blinked slowly on the screen.

_

Pran stretched his fingers and looked at the robot.

[^_^]

/| |\

/ \

A new system message appeared.

PYTHON MODULE PROGRESS: 40%

NEXT MODULE: REPETITION SYSTEM

Pran smiled.

"Repetition… that must mean loops."

The computer printed another message.

SOME TASKS MUST REPEAT MANY TIMES

That was true.

Imagine a program that needs to:

ask a question again if the answer is wrong

count numbers from 1 to 10

keep running a game until the player quits

Without repetition, the programmer would need to write the same instructions again and again.

That would be messy.

Luckily, Python has a way to repeat instructions.

It uses something called a loop.

One of the most common loops is called a while loop.

The computer displayed an example.

number = 0

while number != 5:

number = int(input("Guess the number: "))

Pran studied the code.

The important part was this line.

while number != 5

This means:

Keep running the code while the number is not equal to 5.

So the program will continue asking the question until the correct number is entered.

Pran decided to improve the program a little.

number = 0

while number != 5:

number = int(input("Guess the number: "))

print("Correct!")

He ran the program.

The computer asked:

Guess the number:

Pran typed:

2

The program asked again.

Guess the number:

He typed:

8

Still wrong.

The program asked again.

Guess the number:

Finally he typed:

5

The screen printed:

Correct!

Pran nodded.

"That's clever."

The program kept repeating until the answer was correct.

That is the power of a while loop.

The robot jumped happily.

[^O^]

/| |\

/ \

Another system message appeared.

REPETITION SYSTEM VERIFIED

The computer displayed another example.

A simple counting program.

count = 1

while count <= 5:

print(count)

count = count + 1

Pran ran it.

The output appeared.

1

2

3

4

5

Pran looked carefully at the code.

The key part was this line.

count = count + 1

That line increases the value of the variable.

Without it, the loop would never stop.

The robot quickly displayed a warning.

WARNING: INFINITE LOOP POSSIBLE

Pran laughed.

"If we forget to update the variable, the program could run forever."

That is called an infinite loop.

For example:

count = 1

while count <= 5:

print(count)

In this case, the value of count never changes.

So the condition is always true.

The program would print numbers forever.

The computer screen flickered again.

Another message appeared.

LOOP CONTROL VERIFIED

Then another message.

PYTHON MODULE PROGRESS: 50%

Pran leaned back.

"Halfway through already."

The robot stood proudly.

[^_^]

/| |\

/ \

The computer displayed another system message.

NEXT MODULE: COUNTING SYSTEM

Pran tilted his head.

"Counting system?"

The computer showed another keyword.

FOR LOOP

Pran smiled.

"Another type of loop."

The cursor blinked again.

_

Waiting.

Ready for the next lesson.

Pran placed his hands on the keyboard again.

"Alright," he said.

"Let's see how Python counts."

Next Chapter

Chapter 16 — The Counting Loop

You will learn:

for loops

range()

repeating tasks with counting

More Chapters