Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Nested Loops | Loops
Python Ninja (copy)

Veeg om het menu te tonen

book
Nested Loops

In this chapter, we will explore the concept of nested loops. Nested loops are loops within loops, and they are a powerful tool in programming that allows us to perform repeated actions in a structured way. By using nested loops, we can efficiently navigate and manipulate complex structures, such as grids, which is exactly what our Ninja will be doing in this chapter.

Imagine our Ninja moving through a grid, collecting sushi. The grid is made up of rows and columns, and our goal is to collect all the sushi scattered across this grid. To achieve this, we can use nested loops: one loop to move across the rows and another loop to move through each column.

Let's take a look at an example to understand how nested loops work in the context of our Ninja Game:

py

ninja.py

html

index.html

py

preset.py

copy

In the example above, we have two functions: ninja_controller and loot_column. The ninja_controller function uses a while loop to move the Ninja across the grid from left to right. For each column, it calls the loot_column function, which contains another while loop to move the Ninja upwards, collecting sushi as it goes. Once the Ninja reaches the top of the column, it uses a for loop to move back down to its original position.

By nesting the while loop inside the loot_column function within the while loop of the ninja_controller function, we can efficiently collect all the sushi in each column before moving to the next one.

Taak

Swipe to start coding

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 4
py

ninja.py

html

index.html

py

preset.py

Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

book
Nested Loops

In this chapter, we will explore the concept of nested loops. Nested loops are loops within loops, and they are a powerful tool in programming that allows us to perform repeated actions in a structured way. By using nested loops, we can efficiently navigate and manipulate complex structures, such as grids, which is exactly what our Ninja will be doing in this chapter.

Imagine our Ninja moving through a grid, collecting sushi. The grid is made up of rows and columns, and our goal is to collect all the sushi scattered across this grid. To achieve this, we can use nested loops: one loop to move across the rows and another loop to move through each column.

Let's take a look at an example to understand how nested loops work in the context of our Ninja Game:

py

ninja.py

html

index.html

py

preset.py

copy

In the example above, we have two functions: ninja_controller and loot_column. The ninja_controller function uses a while loop to move the Ninja across the grid from left to right. For each column, it calls the loot_column function, which contains another while loop to move the Ninja upwards, collecting sushi as it goes. Once the Ninja reaches the top of the column, it uses a for loop to move back down to its original position.

By nesting the while loop inside the loot_column function within the while loop of the ninja_controller function, we can efficiently collect all the sushi in each column before moving to the next one.

Taak

Swipe to start coding

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 4
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt