Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn While Loops | Loops
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python Knight

bookWhile Loops

Introduction

Welcome to the chapter on while loops! Here you’ll learn how to use them to help the knight collect coins more efficiently, understand new surrounding-checking methods, and compare strings in Python.

Understanding While Loops

A while loop runs as long as its condition is True. This is useful when you need repeated actions but don’t know the exact number of iterations in advance.

1234
i = 0 while i < 5: print("Incrementing:", i) i += 1
copy

Knight's Surroundings

The knight can inspect nearby cells using: object_up(), object_down(), object_left(), object_right(). Each returns "wall", "coin", or "empty".

String Comparisons

Use == and != to compare strings and decide actions based on what the knight detects.

Example

Here’s an example showing how a while loop helps the knight collect all coins in a column.

knight.py

knight.py

copy

In this example, the collect_column function uses a while loop to collect all the coins in a column. The knight checks if there is coin above it and continues to pick and move up until there is no more coins. After collecting, the knight moves back down to its original position.

Task

Swipe to start coding

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3
knight.py

knight.py

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how the while loop works in the code sample?

What do the `object_up()`, `object_down()`, etc. functions do?

How does string comparison help the knight decide what to do?

close

bookWhile Loops

Swipe to show menu

Introduction

Welcome to the chapter on while loops! Here you’ll learn how to use them to help the knight collect coins more efficiently, understand new surrounding-checking methods, and compare strings in Python.

Understanding While Loops

A while loop runs as long as its condition is True. This is useful when you need repeated actions but don’t know the exact number of iterations in advance.

1234
i = 0 while i < 5: print("Incrementing:", i) i += 1
copy

Knight's Surroundings

The knight can inspect nearby cells using: object_up(), object_down(), object_left(), object_right(). Each returns "wall", "coin", or "empty".

String Comparisons

Use == and != to compare strings and decide actions based on what the knight detects.

Example

Here’s an example showing how a while loop helps the knight collect all coins in a column.

knight.py

knight.py

copy

In this example, the collect_column function uses a while loop to collect all the coins in a column. The knight checks if there is coin above it and continues to pick and move up until there is no more coins. After collecting, the knight moves back down to its original position.

Task

Swipe to start coding

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3
knight.py

knight.py

some-alt