Heute kombinierst du alles, was du gelernt hast:
Am Ende hast du ein kleines Spiel, das du weiter ausbauen kannst.
questions = {
"Was ist die Hauptstadt von Deutschland?": "Berlin",
"2+2 = ?": "4",
"Ist Python eine Programmiersprache?": "Yes"
}
points = 0
for question in questions:
answer = input(question + " ")
if answer == questions[question]:
points += 1
print("Richtig!")
else:
print("Falsch!")
print("Du hast", points, "von", len(questions), "Punkten erreicht!")
Erklärung:
questions speichert Fragen und Antworten
answer = input(question + " ").lower()
if answer == questions[question].lower():
👉 .lower() wandelt alles in Kleinbuchstaben um → vermeidet Fehler bei Groß-/Kleinschreibung
quiz = {
"Mathe": {
"2+2 = ?": "4",
"5*3 = ?": "15"
},
"Allgemein": {
"Hauptstadt von Deutschland?": "Berlin"
}
}
category = input("Wähle eine Kategorie: ")
questions = quiz[category]
quiz = {
"leicht": {
"2+2": "4"
},
"schwer": {
"12*12": "144"
}
}
level = input("Wähle Schwierigkeit (leicht/schwer): ")
questions = quiz[level]
file = open("ergebnisse.txt", "a")
file.write("Punkte: " + str(points) + "\n")
file.close()
import random
quiz = {
"leicht": {
"2+2": "4",
"3+5": "8"
},
"schwer": {
"12*12": "144",
"15*3": "45"
}
}
level = input("Wähle Schwierigkeit (leicht/schwer): ")
questions = quiz[level]
points = 0
for question in questions:
answer = input(question + " ").lower()
if answer == questions[question].lower():
points += 1
print("Richtig!")
else:
print("Falsch!")
print("Du hast", points, "von", len(questions), "Punkten erreicht!")
# Ergebnisse speichern
file = open("ergebnisse.txt", "a")
file.write("Level: " + level + " Punkte: " + str(points) + "\n")
file.close()
Beispiel für Zufall:
import random
question = random.choice(list(questions.keys()))