Advertisement
overactive

asfd

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