Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. # Turniej wiedzy
  2. # Gra odczytujaca dane ze zwyklego pliku tekstoweog
  3.  
  4. import sys, pickle
  5.  
  6. def open_file(file_name, mode):
  7. """
  8. Otwiera plik tekstowy
  9. :param file_name:
  10. :param mode:
  11. :return:
  12. """
  13. try:
  14. the_file = open(file_name, mode)
  15. except IOError as e:
  16. print("Nie można otworzyć pliku", file_name,"program zostanie zakończony\n")
  17. input("\n Aby zakończyć działnie programu, naciśnij klawisz enter,")
  18. sys.exit()
  19. else:
  20. return the_file
  21.  
  22.  
  23. def next_line(the_file):
  24. """
  25. Zwróć kolejny wiersz pliku kwiz po sformatowaniu go.
  26. :param the_file:
  27. :return:
  28. """
  29. line = the_file.readline()
  30. line = line.replace("/", "\n")
  31. return line
  32.  
  33.  
  34. def next_block(the_file):
  35. """
  36. Zwraca cały blok z kategoria, pytaniem, odpowiedziami i objasnieniem
  37. :param the_file:
  38. :return:
  39. """
  40. category = next_line(the_file)
  41. question = next_line(the_file)
  42.  
  43. answers = []
  44. for i in range(4):
  45. answers.append(next_line(the_file))
  46.  
  47. correct = next_line(the_file)
  48. if correct:
  49. correct = correct[0]
  50.  
  51. points = next_line(the_file)
  52. explanation = next_line(the_file)
  53.  
  54. return category, question, answers, correct, points, explanation
  55.  
  56.  
  57. def welcome(title):
  58. """
  59. Wita gracza i zwraca tytul odcinka
  60. :param title:
  61. :return:
  62. """
  63. print("Witaj w tutnieju wiedzy\n")
  64. print("Tytul odcinka to:",title)
  65.  
  66.  
  67. def add_score(name,score):
  68. """
  69. Dodaje wynik do osobnego pliku txt.
  70. :param name:
  71. :param score:
  72. :return:
  73. """
  74. f = open("scores_game.dat", "ab")
  75. scores = [name,score]
  76. pickle.dump(scores, f)
  77. f.close()
  78.  
  79.  
  80. def main():
  81. trivia_file = open_file("quiz.txt", "r")
  82. title = next_line(trivia_file)
  83. welcome(title)
  84. score = 0
  85. # pobierz pierwszy blok
  86. category, question, answers, correct, points, explanation = next_block(trivia_file)
  87. while category:
  88. print(category)
  89. print(question)
  90. for i in range(4):
  91. print("\t",i + 1,"-", answers[i])
  92.  
  93. answer = input("Jaka jest Twoja odpowiedz?: ")
  94. if answer == correct:
  95. print("Odpowiedz prawidłowa", end = " ")
  96. score = score + int(points)
  97. else:
  98. print("Odpowiedz nieprawidlowa", end = " ")
  99. print(explanation)
  100. print("Wynik:",score)
  101.  
  102. category, question, answers, correct, points, explanation = next_block(trivia_file)
  103.  
  104. trivia_file.close()
  105. print("To koniec pytan. Twój wynik to:",score)
  106. name = str(input("Podaj swoje imie: "))
  107. add_score(name,score)
  108.  
  109.  
  110. main()
  111. print("Wyniki gry:")
  112. f = open("scores_game.dat", "rb")
  113. scores = pickle.load(f)
  114. print(scores)
  115. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement