14. Fortgeschrittene Schleifen

../../_images/nested_loops.png

Spiele verwenden eine Menge an komplexen Schleifen. Dies ist ein „herausforderndes“ Kapitel, um zu lernen wie ein Experte Schleifen einzusetzen. Wenn du die Probleme in diesem Kapitel verstehen kannst, kannst du dich am Ende ein Schleifenexperte nennen.

At the very least, make sure you can write out the answers for the first eight problems. That will give you enough knowledge to continue this book.

You can try solving these on your computer, or on-line with repl.it. Scroll down to the „05“ set of problems.

14.2. Fortgeschrittene Schleifen-Probleme

14.2.1. Problem 1

Write code that will print ten asterisks (*) in a row:

* * * * * * * * * *

Write code to print the asterisks using a for loop. Print each asterisk individually, don’t just print all ten with one „print“ statement. This problem can be completed in two lines of code using one for loop and one print statement.

Remember, don’t look at the answer until you’ve figured it out yourself, or you have been trying for 5-10 minutes. (I don’t recommend waiting longer than ten minutes.)

Answer to Problem 1

14.2.2. Problem 2

Schreibe Code, der das folgende ausgibt:

* * * * * * * * * *
* * * * *
* * * * * * * * * * * * * * * * * * * *

This is just like the prior problem, but also printing five and twenty stars. Copy and paste from the prior problem, adjusting the for loop as needed. Don’t forget how Moving to the Next Line.

Answer to Problem 2

14.2.3. Problem 3

Verwende zwei for-Schleifen, eine in die andere geschachtelt, um das folgende 10x10-Rechteck auszugeben:

* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *

To start, take a look at Problem 1. The code in Problem 1 generates one line of asterisks. It needs to be repeated ten times. You’ll also need to move to the next line after each row has printed. Work on this problem for at least ten minutes before looking at the answer.

Answer to Problem 3

14.2.4. Problem 4

Verwende zwei for-Schleifen, eine in die andere geschachtelt, um das folgende 5x10-Rechteck auszugeben.

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

This is a lot like the prior problem. Experiment with the ranges on the loops to find exactly what the numbers passed to the range function control.

Answer to Problem 4

14.2.5. Problem 5

Use two for loops, one of them nested, to print the following 20x5 rectangle:

* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *

Es ähnelt Problem 3 und 4, aber mit verschiedenen Werten für range.

Answer to Problem 5

14.2.6. Problem 6

Schreibe Code, der das folgende ausgibt:

0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

Use two nested loops. Print the first line with a loop. Don’t use code like this:

print("0 1 2 3 4 5 6 7 8 9")

Achtung

First, create a loop that prints the first line. Then enclose it in another loop that repeats the line 10 times. Use either i or j variables for what the program prints. This example and the next one helps reinforce what those index variables are doing.

Arbeite an diesem Problem mindestens zehn Minuten, bevor du die Lösung nachsiehst. Der Prozess, zehn Minuten an dem Problem zu knabbern, ist wichtiger als die Lösung selbst.

Answer to Problem 6

14.2.7. Problem 7

Passe die Ausgabe des vorigen Programms an.

0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9

Answer to Problem 7

14.2.8. Problem 8

Schreibe Code, der das folgende ausgibt:

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9

Tipp: Das ist auch nur das Problem 6, aber in der Schleife wird nicht mehr eine feste Anzahl wiederholt. Verwende nicht range(10), sondern passe den Bereich an.

Answer to Problem 8

Make sure you can write out the code for this and the prior problems. Repeat until you can get it without looking up the answer. Yes, this practice is work, but it will pay off later and you’ll save time in the long run.

14.2.9. Problem 9

Schreibe Code, der das folgende ausgibt:

0 1 2 3 4 5 6 7 8 9
  0 1 2 3 4 5 6 7 8
    0 1 2 3 4 5 6 7
      0 1 2 3 4 5 6
        0 1 2 3 4 5
          0 1 2 3 4
            0 1 2 3
              0 1 2
                0 1
                  0

Dieses Problem ist schwierig. Tipp: Es werden zwei Schleifen benötigt, die in einer äußeren, die jede Zeile steuert, geschachtelt sind. Zuerst gibt eine Schleife Leerzeichen aus, danach gibt eine Schleife die Zahlen aus. Wiederhole dies für jede Zeile. Für den Anfang versuche einfach zu schreiben, was die innere Schleife ausgibt:

0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0

Wenn das dann funktioniert, dann füge dann eine Schleife zwischen die äußere Schleife und die schon existierende innere Schleife ein. Verwende diese neue Schleife um genügend Leerzeichen zum Einrücken einzufügen.

Answer to Problem 9

14.2.10. Problem 10

Schreibe Code, der das folgende ausgibt (Die Ausrichtung richtig hinzubekommen ist schwierig, aber es sollten wenigstens die Zahlen ausgegeben werden):

1  2  3  4  5  6  7  8  9
2  4  6  8 10 12 14 16 18
3  6  9 12 15 18 21 24 27
4  8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

Tipp: Fange damit an, den Code aus Problem 1 anzupassen, um das folgende auszugeben:

0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
0 3 6 9 12 15 18 21 24 27
0 4 8 12 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81

Passe danach den Code an, um auszugeben:

1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

Finally, use an 11if`` to print spaces if the number being printed is less than 10. (Or use string formatting if you are already familar with that.)

Answer to Problem 10

14.2.11. Problem 11

Schreibe Code, der das folgende ausgibt:

                1
              1 2 1
            1 2 3 2 1
          1 2 3 4 3 2 1
        1 2 3 4 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
    1 2 3 4 5 6 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

Tipp: Schreibe als erstes Code, um auszugeben:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9

Schreibe danach Code um auszugeben:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

Beende das durch Hinzufügen von Leerzeichen für die endgültige Lösung.

Answer to Problem 11

14.2.12. Problem 12

Schreibe Code, der das folgende ausgibt:

                1
              1 2 1
            1 2 3 2 1
          1 2 3 4 3 2 1
        1 2 3 4 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
    1 2 3 4 5 6 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8
    1 2 3 4 5 6 7
      1 2 3 4 5 6
        1 2 3 4 5
          1 2 3 4
            1 2 3
              1 2
                1

Dies kann durch eine Kombination von Problem 11 und 9 erreicht werden.

Answer to Problem 12

14.2.13. Problem 13

Schreibe Code, der das folgende ausgibt:

                1
              1 2 1
            1 2 3 2 1
          1 2 3 4 3 2 1
        1 2 3 4 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
    1 2 3 4 5 6 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 6 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
        1 2 3 4 5 4 3 2 1
          1 2 3 4 3 2 1
            1 2 3 2 1
              1 2 1
                1

Answer to Problem 13