Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. import random
  2. import sys
  3. import argparse
  4.  
  5. MAX_PLAYERS = 3
  6. MIN_PLAYERS = 1
  7.  
  8. class Question():
  9.     def __init__(self, q, a, c):
  10.         self.question = q
  11.         self.answers = a
  12.         self.correct = c
  13.  
  14. class Quiz():
  15.     def __init__(self, fileName, playersNo):
  16.         self.playersNo = playersNo
  17.         self.entries = self.loadQuestions(fileName)
  18.         print('Witamy w grze')
  19.         print('Liczba graczy: {}'.format(self.playersNo))
  20.  
  21.     def loadQuestions(self, fileName):
  22.         entries = []
  23.         try:
  24.             with open(fileName, 'r') as file:
  25.                 data = file.readlines()
  26.         except IOError:
  27.           print ('Nie można otworzyc pliku "{}"'.format(fileName))
  28.           sys.exit(1)
  29.        
  30.         for i in range(0, len(data), 4):
  31.             q = data[i].rstrip()
  32.             a = data[i + 1].rstrip().split(';')
  33.             c = data[i + 2].rstrip()
  34.             entries.append(Question(q, a, c))
  35.  
  36.         return entries
  37.        
  38.     def askQuestion(self, question, answers):
  39.         print(question)
  40.         random.shuffle(answers)
  41.         self.printAnswers(answers)
  42.  
  43.         print('Twoja odpowiedź to (wpisz literę):')
  44.         self.userAnswer = input()
  45.        
  46.     def printAnswers(self, answers):
  47.         for i in range(len(answers)):
  48.             print('{}) {}'.format(chr(i + 97), answers[i]))
  49.  
  50.     def getCorrectLetter(self, answers, correct):
  51.         return chr(answers.index(correct) + 97)
  52.        
  53.     def printScore(self):
  54.         questionCount = len(self.entries)
  55.         print('Koniec gry!\n')
  56.  
  57.         playersHeader = ''
  58.         for i in range(self.playersNo):
  59.             playersHeader += ' Gracz {} |'.format(i + 1)
  60.  
  61.         header = 'Numer pytania |' + playersHeader
  62.         print(header)
  63.         print('-' * len(header))
  64.  
  65.         for question in range(questionCount):
  66.             results = ''
  67.             for player in range(self.playersNo):
  68.                 results += ' {:^7} |'.format(self.points[player][question])
  69.             print(' {:^12} |'.format(question + 1) + results)
  70.  
  71.     def start(self):
  72.         self.points={}
  73.         for i in range(self.playersNo):
  74.             self.points[i] = []
  75.             for entry in self.entries:
  76.                 self.askQuestion(entry.question, entry.answers)
  77.                 correctLetter = self.getCorrectLetter(entry.answers, entry.correct)
  78.                 if correctLetter == self.userAnswer:
  79.                     self.points[i].append(1)
  80.                 else:
  81.                     self.points[i].append(0)
  82.                
  83.                 print('')
  84.  
  85.  
  86. choiceRange = range(MIN_PLAYERS, MAX_PLAYERS + 1)
  87. parser = argparse.ArgumentParser()
  88. parser.add_argument('file',
  89.                     help='Wprowadź ściężkę do pliku z pytaniami')
  90. parser.add_argument('-p','--players',type=int, choices=choiceRange, default=1,
  91.                      help='podaj liczbę graczy z zakresu <1,3>')
  92. args = parser.parse_args()
  93.  
  94. file = args.file
  95. players = args.players
  96.  
  97. game = Quiz(file, players)
  98. game.start()
  99. game.printScore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement