overactive

sdasd

Jul 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. import random
  2.  
  3. class Question():
  4.     def __init__(self, q, a, c):
  5.         self.question = q
  6.         self.answers = a
  7.         self.correct = c
  8.  
  9. class Quiz():
  10.     def __init__(self, entries):
  11.         self.entries = entries
  12.         self.points = 0
  13.         print('Witamy w grze')
  14.        
  15.     def askQuestion(self, question, answers):
  16.         print(question)
  17.         random.shuffle(answers)
  18.         self.printAnswers(answers)
  19.  
  20.         print('Twoja odpowiedź to: (wpisz literę)')
  21.         self.userAnswer = input()
  22.        
  23.     def printAnswers(self, answers):
  24.         for i in range(len(answers)):
  25.             print(chr(i + 97),')', answers[i])
  26.  
  27.     def getCorrect(self, answers, correct):
  28.         self.correctIndex = answers.index(correct)
  29.         return self.correctIndex
  30.        
  31.     def addPoints(self):
  32.         if chr(self.correctIndex + 97) == self.userAnswer:
  33.             self.points += 1
  34.    
  35.     def printPoints(self):
  36.         print('Koniec gry!')
  37.         print('Zdobyłeś:')
  38.         print(self.points)
  39.  
  40.     def start(self):
  41.         for entry in self.entries:
  42.             self.askQuestion(entry.question, entry.answers)
  43.             self.getCorrect(entry.answers, entry.correct)
  44.             self.addPoints()
  45.             print('')
  46.  
  47. def loadQuestions(fileName):
  48.     entries = []
  49.  
  50.     with open(fileName, 'r') as file:
  51.         data = file.readlines()
  52.        
  53.         for i in range(0, len(data), 4):
  54.             q = data[i].rstrip()
  55.             a = data[i + 1].rstrip().split(';')
  56.             c = data[i + 2].rstrip()
  57.             entries.append(Question(q, a, c))
  58.    
  59.     return entries
  60.  
  61. game = Quiz(loadQuestions('quiz.txt'))
  62. game.start()
  63. game.printPoints()
Advertisement
Add Comment
Please, Sign In to add comment