Übung 10 – if und else: Lösungen
Ziel dieser Übung
Ihr Programm führt Zeilen nur aus, wenn eine Bedingung wahr ist (if), und sonst etwas anderes (else).
So arbeiten Sie: Die Aufgabe 2 steht in der
Reflexion zu Sitzung 3. Programmieren Sie dort zuerst selbst und vergleichen Sie erst danach mit der Lösung hier. Ihre Lösung darf anders aussehen – wichtig ist, dass sie für alle Eingaben das Richtige ausgibt.
1. Aufbau von if
temperatur = 25
if temperatur > 20:
print("Heute reicht ein T-Shirt.")
print("Schönen Tag!")
Heute reicht ein T-Shirt.
Schönen Tag!
- Nach der Bedingung steht ein Doppelpunkt.
- Alles, was zur Bedingung gehört, ist um 4 Leerzeichen eingerückt.
- Die letzte Zeile ist nicht eingerückt und läuft deshalb immer.
2. else: sonst
else: hat keine eigene Bedingung. Es läuft genau dann, wenn die Bedingung bei if falsch ist. Neu ist %: Es liefert den Rest einer Division, z. B. 7 % 2 = 1.
Fehlt der Doppelpunkt, meldet Python SyntaxError: expected ':'. Fehlt die Einrückung, kommt IndentationError: expected an indented block.
Aufgabe 2a · Gerade oder ungerade Übung
Eine ganze Zahl einlesen und ausgeben, ob sie gerade oder ungerade ist.
Lösung anzeigen
zahl = int(input("Zahl: "))
if zahl % 2 == 0:
print(f"{zahl} ist gerade.")
else:
print(f"{zahl} ist ungerade.")
Zahl: 7
7 ist ungerade.
Und mit einer geraden Zahl:
zahl = int(input("Zahl: "))
if zahl % 2 == 0:
print(f"{zahl} ist gerade.")
else:
print(f"{zahl} ist ungerade.")
Zahl: 10
10 ist gerade.
Aufgabe 2b · Bestanden oder nicht Übung
Ab 50 Punkten „Bestanden“, sonst „Leider nicht bestanden“.
Lösung anzeigen
punkte = int(input("Punkte: "))
if punkte >= 50:
print("Bestanden")
else:
print("Leider nicht bestanden")
Punkte: 50
Bestanden
Genau 50 Punkte reichen, deshalb >=. Mit > 50 wäre jemand mit 50 Punkten durchgefallen – ein typischer Logikfehler, den nur ein Test mit genau 50 findet.
Weiter zu Übung 11: elif, and, or →
Exercise 10 – if and else: Solutions
Goal of this exercise
Your program runs lines only if a condition is true (if), and otherwise something else (else).
How to work: Task 2 is in the
reflection on session 3. Program it there yourself first and only then compare with the solution here. Your solution may look different – what matters is that it gives the right output for every input.
1. How if is built
temperatur = 25
if temperatur > 20:
print("Heute reicht ein T-Shirt.")
print("Schönen Tag!")
Heute reicht ein T-Shirt.
Schönen Tag!
- The condition is followed by a colon.
- Everything that belongs to the condition is indented by 4 spaces.
- The last line is not indented, so it always runs.
2. else: otherwise
else: has no condition of its own. It runs exactly when the condition at if is false. New is %: it gives the remainder of a division, e.g. 7 % 2 = 1.
If the colon is missing, Python reports SyntaxError: expected ':'. If the indentation is missing, you get IndentationError: expected an indented block.
Task 2a · Even or odd Practice
Read a whole number and print whether it is even or odd.
Show solution
zahl = int(input("Zahl: "))
if zahl % 2 == 0:
print(f"{zahl} ist gerade.")
else:
print(f"{zahl} ist ungerade.")
Zahl: 7
7 ist ungerade.
And with an even number:
zahl = int(input("Zahl: "))
if zahl % 2 == 0:
print(f"{zahl} ist gerade.")
else:
print(f"{zahl} ist ungerade.")
Zahl: 10
10 ist gerade.
Task 2b · Passed or not Practice
From 50 points “Bestanden” (passed), otherwise “Leider nicht bestanden”.
Show solution
punkte = int(input("Punkte: "))
if punkte >= 50:
print("Bestanden")
else:
print("Leider nicht bestanden")
Punkte: 50
Bestanden
Exactly 50 points are enough, hence >=. With > 50 someone with 50 points would fail – a typical logic error that only a test with exactly 50 reveals.
On to exercise 11: elif, and, or →