Übung 13 – for und range
Ziel dieser Übung
Sie wiederholen Zeilen eine feste Anzahl von Malen mit for und range und sammeln Ergebnisse in einer Variablen.
So arbeiten Sie: Bearbeiten Sie die Schritte der Reihe nach. Ihre Tabellen und Programme tragen Sie in die
Reflexion zu Sitzung 4 ein. Öffnen Sie die Hilfen erst, wenn Sie nicht weiterkommen: zuerst den Tipp, dann das Gerüst, zuletzt die Lösung. Ihre Lösung darf anders aussehen – wichtig ist, dass sie für alle Testeingaben das Richtige ausgibt.
1. for und range
for i in range(1, 4):
print(f"Runde {i}")
Runde 1
Runde 2
Runde 3
| Aufruf | Zahlen | Bedeutung |
|---|
range(5) | 0 1 2 3 4 | von 0 bis vor 5 |
range(1, 6) | 1 2 3 4 5 | von 1 bis vor 6 |
range(0, 10, 2) | 0 2 4 6 8 | in Zweierschritten |
range(5, 0, -1) | 5 4 3 2 1 | rückwärts |
while oder for? Wissen Sie vorher, wie oft wiederholt wird, nehmen Sie for. Wissen Sie nur, wann Schluss ist, nehmen Sie while.
2. Aufgaben
Aufgabe 2a · Einmaleins Pflicht, ca. 7 Min.
Ein Lernprogramm soll die Einmaleins-Reihe einer Zahl von 1 bis 10 ausgeben.
Ihre Aufgabe
- Tragen Sie für die Reihe
7 ein, welche Zeilen an den genannten Stellen stehen müssen.| Zeile | Ausgabe (erwartet) |
|---|
1. | |
2. | |
5. | |
10. | |
- Schreiben Sie
einmaleins.py: Es fragt mit Welche Reihe? nach der Zahl und gibt mit for und range zehn Zeilen aus. - Die Ausgabe beginnt und endet für die Reihe 7 genau so:
Welche Reihe? 7
1 x 7 = 7
2 x 7 = 14
…
10 x 7 = 70
- Testen Sie mit
7 und 3 und vergleichen Sie mit Ihrer Tabelle.
Fertig sind Sie, wenn genau zehn Zeilen erscheinen, die erste mit 1 x und die letzte mit 10 x.
Tipp
Für die Zahlen 1 bis 10 brauchen Sie range(1, 11): Die Grenze gehört nicht dazu. Das Ergebnis rechnen Sie direkt im f-String aus: {i * zahl}.
Gerüst
Ersetzen Sie jedes ___ durch den passenden Ausdruck.
zahl = int(input("Welche Reihe? "))
for i in range(___, ___):
print(f"{i} x {zahl} = {___}")
Lösung
| Zeile | Ausgabe |
|---|
| 1. | 1 x 7 = 7 |
| 2. | 2 x 7 = 14 |
| 5. | 5 x 7 = 35 |
| 10. | 10 x 7 = 70 |
zahl = int(input("Welche Reihe? "))
for i in range(1, 11):
print(f"{i} x {zahl} = {i * zahl}")
Welche Reihe? 3
1 x 3 = 3
2 x 3 = 6
3 x 3 = 9
4 x 3 = 12
5 x 3 = 15
6 x 3 = 18
7 x 3 = 21
8 x 3 = 24
9 x 3 = 27
10 x 3 = 30
Mit range(10) liefe i von 0 bis 9: Die erste Zeile wäre dann 0 x 3 = 0 und 10 x 3 fehlte.
Aufgabe 2b · Summe von 1 bis n Pflicht, ca. 8 Min.
Ein Programm soll alle Zahlen von 1 bis n zusammenzählen, z. B. für n = 4: 1 + 2 + 3 + 4 = 10.
Ihre Aufgabe
- Rechnen Sie die erwarteten Summen von Hand aus.
| n | Summe (erwartet) | Programm stimmt? |
|---|
1 | | |
5 | | |
10 | | |
100 | | |
- Schreiben Sie
summe.py: Es fragt mit Bis zu welcher Zahl? nach n und rechnet mit einer Sammelvariablen summe und einer for-Schleife. - Geben Sie das Ergebnis genau so aus:
Bis zu welcher Zahl? 4
Die Summe von 1 bis 4 ist 10.
- Testen Sie alle vier Werte und vermerken Sie in der Tabelle, ob das Programm stimmt. Wie lange hätten Sie für n = 100 von Hand gebraucht?
Fertig sind Sie, wenn alle vier Summen stimmen – auch für n = 1.
Zusatz für Schnelle: Schreiben Sie treppe.py, das für eine Stufenzahl eine Treppe aus Sternchen ausgibt. Tipp: "*" * 3 ergibt ***.
Tipp
Vor der Schleife steht summe = 0. In jeder Runde kommt die aktuelle Zahl dazu: summe = summe + zahl. Damit n selbst mitgezählt wird, endet range bei n + 1.
Gerüst
Ersetzen Sie jedes ___ durch den passenden Ausdruck.
n = int(input("Bis zu welcher Zahl? "))
summe = ___
for zahl in range(1, ___):
summe = ___
print(f"Die Summe von 1 bis {n} ist {summe}.")
Lösung
n = int(input("Bis zu welcher Zahl? "))
summe = 0
for zahl in range(1, n + 1):
summe = summe + zahl
print(f"Die Summe von 1 bis {n} ist {summe}.")
Bis zu welcher Zahl? 100
Die Summe von 1 bis 100 ist 5050.
Der junge Carl Friedrich Gauß soll diese Aufgabe ohne Schleife gelöst haben: 1 + 100, 2 + 99, … ergibt 50-mal 101 = 5050. Python braucht dafür keine Idee, nur die Schleife.
Zusatz:
n = int(input("Wie viele Stufen? "))
for stufe in range(1, n + 1):
print("*" * stufe)
Wie viele Stufen? 4
*
**
***
****
Weiter zu Übung 14: Listen →
Exercise 13 – for and range
Goal of this exercise
You repeat lines a fixed number of times with for and range and collect results in a variable.
How to work: work through the steps in order. Enter your tables and programs in the
reflection on session 4. Open the help only when you are stuck: first the tip, then the scaffold, finally the solution. Your solution may look different – what matters is that it gives the right output for every test input.
1. for and range
for i in range(1, 4):
print(f"Runde {i}")
Runde 1
Runde 2
Runde 3
| Call | Numbers | Meaning |
|---|
range(5) | 0 1 2 3 4 | from 0 up to before 5 |
range(1, 6) | 1 2 3 4 5 | from 1 up to before 6 |
range(0, 10, 2) | 0 2 4 6 8 | in steps of two |
range(5, 0, -1) | 5 4 3 2 1 | backwards |
while or for? If you know in advance how often to repeat, use for. If you only know when to stop, use while.
2. Tasks
Task 2a · Times table Required, approx. 7 min
A learning program should print the times table of a number from 1 to 10.
Your task
- For the
7 times table, enter which lines must appear at the given positions.| Line | Output (expected) |
|---|
1. | |
2. | |
5. | |
10. | |
- Write
einmaleins.py: it asks for the number with Welche Reihe? and prints ten lines with for and range. - For the 7 times table, the output starts and ends exactly like this:
Welche Reihe? 7
1 x 7 = 7
2 x 7 = 14
…
10 x 7 = 70
- Test with
7 and 3 and compare with your table.
You are done when exactly ten lines appear, the first with 1 x and the last with 10 x.
Tip
For the numbers 1 to 10 you need range(1, 11): the end is not included. Calculate the result straight in the f-string: {i * zahl}.
Scaffold
Replace every ___ with the right expression.
zahl = int(input("Welche Reihe? "))
for i in range(___, ___):
print(f"{i} x {zahl} = {___}")
Solution
| Line | Output |
|---|
| 1. | 1 x 7 = 7 |
| 2. | 2 x 7 = 14 |
| 5. | 5 x 7 = 35 |
| 10. | 10 x 7 = 70 |
zahl = int(input("Welche Reihe? "))
for i in range(1, 11):
print(f"{i} x {zahl} = {i * zahl}")
Welche Reihe? 3
1 x 3 = 3
2 x 3 = 6
3 x 3 = 9
4 x 3 = 12
5 x 3 = 15
6 x 3 = 18
7 x 3 = 21
8 x 3 = 24
9 x 3 = 27
10 x 3 = 30
With range(10), i would run from 0 to 9: the first line would be 0 x 3 = 0 and 10 x 3 would be missing.
Task 2b · Sum from 1 to n Required, approx. 8 min
A program should add up all numbers from 1 to n, e.g. for n = 4: 1 + 2 + 3 + 4 = 10.
Your task
- Work out the expected sums by hand.
| n | Sum (expected) | Program correct? |
|---|
1 | | |
5 | | |
10 | | |
100 | | |
- Write
summe.py: it asks for n with Bis zu welcher Zahl? and calculates with an accumulator summe and a for loop. - Print the result exactly like this:
Bis zu welcher Zahl? 4
Die Summe von 1 bis 4 ist 10.
- Test all four values and note in the table whether the program is correct. How long would n = 100 have taken you by hand?
You are done when all four sums are right – including n = 1.
Extra for the faster ones: write treppe.py, which prints a staircase of stars for a number of steps. Tip: "*" * 3 gives ***.
Tip
Before the loop: summe = 0. In every round the current number is added: summe = summe + zahl. So that n itself is included, range ends at n + 1.
Scaffold
Replace every ___ with the right expression.
n = int(input("Bis zu welcher Zahl? "))
summe = ___
for zahl in range(1, ___):
summe = ___
print(f"Die Summe von 1 bis {n} ist {summe}.")
Solution
n = int(input("Bis zu welcher Zahl? "))
summe = 0
for zahl in range(1, n + 1):
summe = summe + zahl
print(f"Die Summe von 1 bis {n} ist {summe}.")
Bis zu welcher Zahl? 100
Die Summe von 1 bis 100 ist 5050.
The young Carl Friedrich Gauss is said to have solved this without a loop: 1 + 100, 2 + 99, … gives 50 times 101 = 5050. Python needs no clever idea, just the loop.
Extra:
n = int(input("Wie viele Stufen? "))
for stufe in range(1, n + 1):
print("*" * stufe)
Wie viele Stufen? 4
*
**
***
****
On to exercise 14: lists →