Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- class Question():
- def __init__(self, q, a, c):
- self.question = q
- self.answers = a
- self.correct = c
- class Quiz():
- def __init__(self, entries):
- self.entries = entries
- self.points = 0
- print('Witamy w grze')
- def askQuestion(self, question, answers, correct):
- print(question)
- self.printAnswers(answers)
- print('Twoja odpowiedź to: (wpisz literę)')
- self.userAnswer = input()
- self.getCorrect(answers, correct)
- def printAnswers(self, answers):
- random.shuffle(answers)
- for i in range(len(answers)):
- print(chr(i + 97), answers[i])
- def getCorrect(self, answers, correct):
- correctIndex = answers.index(correct)
- if chr(correctIndex + 97) == self.userAnswer:
- self.points += 1
- def printPoints(self):
- print('Koniec gry!')
- print('Zdobyłeś:')
- print(self.points)
- def start(self):
- for entry in self.entries:
- self.askQuestion(entry.question, entry.answers, entry.correct)
- print('')
- def loadQuestions(fileName):
- entries = []
- with open(fileName, 'r') as file:
- data = file.readlines()
- for i in range(0, len(data), 4):
- q = data[i].rstrip()
- a = data[i + 1].rstrip().split(';')
- c = data[i + 2].rstrip()
- entries.append(Question(q, a, c))
- return entries
- game = Quiz(loadQuestions('quiz.txt'))
- game.start()
- game.printPoints()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement