Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 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 .
  70. :param name:
  71. :param score:
  72. :return:
  73. """
  74.  
  75. my_list = []
  76. scores = (name,score)
  77. my_list.append(scores)
  78. with open("score_games.dat", "ab") as f:
  79. pickle.dump(my_list, f)
  80. f.close()
  81.  
  82.  
  83. def main():
  84. trivia_file = open_file("quiz.txt", "r")
  85. title = next_line(trivia_file)
  86. welcome(title)
  87. score = 0
  88. # pobierz pierwszy blok
  89. category, question, answers, correct, points, explanation = next_block(trivia_file)
  90. while category:
  91. print(category)
  92. print(question)
  93. for i in range(4):
  94. print("\t",i + 1,"-", answers[i])
  95.  
  96. answer = input("Jaka jest Twoja odpowiedz?: ")
  97. if answer == correct:
  98. print("Odpowiedz prawidłowa", end = " ")
  99. score = score + int(points)
  100. else:
  101. print("Odpowiedz nieprawidlowa", end = " ")
  102. print(explanation)
  103. print("Wynik:",score)
  104.  
  105. category, question, answers, correct, points, explanation = next_block(trivia_file)
  106.  
  107. trivia_file.close()
  108. print("To koniec pytan. Twój wynik to:",score)
  109. name = str(input("Podaj swoje imie: "))
  110. add_score(name,score)
  111.  
  112.  
  113. main()
  114. print("Wyniki gry:")
  115. with open("score_games.dat", "rb") as f:
  116. my_list_1 = pickle.load(f)
  117. print(my_list_1)
  118.  
  119.  
  120. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement