Hab das ganze mal objektorientiert gebaut so spasseshalber, weiss nicht, obs gut geworden ist, guckts euch einfach mal an...
Code: Alles auswählen
#!/usr/bin/env python
#
# kleines quiz
class Question:
def __init__(self, text, answer):
self._text = text
self._answers = []
self._correctAnswer = answer
def addAnswer(self, text):
self._answers.append(text)
def ask(self, num):
points = 0
while(1):
print "Frage %d : %s" % (num ,self._text)
for i in range(0, len(self._answers)):
print "Antwort : %d : %s" % (i, self._answers[i])
userinput = raw_input("???: ")
if int(userinput) == self._correctAnswer:
print "Richtig... ;-)"
points = 1
break
else:
if not raw_input("Leider falsch... nochmal[j/n]??") == 'j':
break
return(points)
class Quiz:
def __init__(self):
self._questions = []
self._points = 0
def addQuestion(self, text, answers, correct):
q = Question(text, correct)
for answer in answers:
q.addAnswer(answer)
self._questions.append(q)
def run(self):
for i in range(0, len(self._questions)):
self._points += self._questions[i].ask(i)
print "you reached %d points..." % self._points
# *** MAIN ***
quiz = Quiz()
quiz.addQuestion(
"Was ist 10 hoch 0",["0", "1", "10", "1000"], 1
)
quiz.addQuestion(
"Wann entdeckte Columbus Amerika", ["1939", "1789", "1492", "1117"], 2
)
quiz.addQuestion(
"Wie lautet Fermats letzter Satz?",
["a^2 + b^2 = c^2", "a^2 / b^2 = c^2", "a^3 + b^3 = c^3", "b^2 / (a^2 / 2) = c^2 / 3"],
2
)
quiz.run()
Fuer Anregungen immer dankbar...
Sebastian