Advertisement
Voria

dump\load

Feb 21st, 2023
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.98 KB | Help | 0 0
  1. import sys
  2. import pickle
  3.  
  4.  
  5. def open_file(file_name, mode):
  6.     """Open a file."""
  7.     try:
  8.         the_file = open(file_name, mode)
  9.     except IOError as e:
  10.         print("Unable to open the file", file_name, "Ending program.\n", e)
  11.         input("\n\nPress the enter key to exit.")
  12.         sys.exit()
  13.     else:
  14.         return the_file
  15.  
  16.  
  17. def next_line(the_file):
  18.     line = the_file.readline()
  19.     line = line.replace("/", "\n")
  20.     return line
  21.  
  22.  
  23. def next_block(the_file):
  24.     category = next_line(the_file)
  25.  
  26.     question = next_line(the_file)
  27.  
  28.     answers = []
  29.     for i in range(4):
  30.         answers.append(next_line(the_file))
  31.  
  32.     correct = next_line(the_file)
  33.     if correct:
  34.         correct = correct[0]
  35.  
  36.     explanation = next_line(the_file)
  37.  
  38.     rating = next_line(the_file)
  39.  
  40.     return category, question, answers, correct, explanation, rating
  41.  
  42.  
  43. def welcome(title):
  44.     """Welcome the player and get his/her name."""
  45.     print("\t\tWelcome to Trivia Challenge!\n")
  46.     print("\t\t", title, "\n")
  47.  
  48.  
  49. def main():
  50.     trivia_file = open_file("trivia.txt", "r")
  51.     title = next_line(trivia_file)
  52.     welcome(title)
  53.     score = 0
  54.  
  55.     # get first block
  56.     #    category, question, answers, correct, explanation = next_block(trivia_file)
  57.     category, question, answers, correct, explanation, rating = next_block(trivia_file)
  58.     while category:
  59.         # ask a question
  60.         print(category)
  61.         print(question)
  62.         for i in range(4):
  63.             print("\t", i + 1, "-", answers[i])
  64.  
  65.         # get answer
  66.         answer = input("What's your answer?: ")
  67.  
  68.         # check answer
  69.         if answer == correct:
  70.             print("\nRight!", end=" ")
  71.             score += int(rating)
  72.         else:
  73.             print("\nWrong.", end=" ")
  74.         print(explanation)
  75.         print("Score:", score, "\n\n")
  76.         # get next block
  77.         category, question, answers, correct, explanation, rating = next_block(trivia_file)
  78.     trivia_file.close()
  79.     print("That was the last question!")
  80.     print("You're final score is", score)
  81.     name = input("Enter player name: ")
  82.     records = {name: score}
  83.     f = open("records.bin", "ab")
  84.     pickle.dump(records, f)
  85.     f.close()
  86.     f = open("records.bin", "rb")
  87.     high_scores = pickle.load(f)
  88.     f.close()
  89.     print(high_scores)
  90.  
  91.  
  92. main()
  93. input("\n\nPress the enter key to exit.")
  94.  
  95. __________________________________________________
  96.  
  97. # trivia.txt
  98.  
  99. An Episode You Can't Refuse
  100. On the Run With a Mammal
  101. Let's say you turn state's evidence and need to "get on the lamb." If you wait /too long, what will happen?
  102. You'll end up on the sheep
  103. You'll end up on the cow
  104. You'll end up on the goat
  105. You'll end up on the emu
  106. 1
  107. A lamb is just a young sheep.
  108. 1
  109. The Godfather Will Get Down With You Now
  110. Let's say you have an audience with the Godfather of Soul. How would it be /smart to address him?
  111. Mr. Richard
  112. Mr. Domino
  113. Mr. Brown
  114. Mr. Checker
  115. 3
  116. James Brown is the Godfather of Soul.
  117. 2
  118. That's Gonna Cost Ya
  119. If you paid the Mob protection money in rupees, what business would you most /likely be insuring?
  120. Your tulip farm in Holland
  121. Your curry powder factory in India
  122. Your vodka distillery in Russian
  123. Your army knife warehouse in Switzerland
  124. 2
  125. The Rupee is the standard monetary unit of India.
  126. 3
  127. Keeping It the Family
  128. If your mother's father's sister's son was in "The Family," how are you /related to the mob?
  129. By your first cousin once removed
  130. By your first cousin twice removed
  131. By your second cousin once removed
  132. By your second cousin twice removed
  133. 1
  134. Your mother's father's sister is her aunt -- and her son is your /mother's first cousin. Since you and your mother are exactly one generation /apart, her first cousin is your first cousin once removed.
  135. 4
  136. A Maid Man
  137. If you were to literally launder your money, but didn't want the green in your /bills to run, what temperature should you use?
  138. Hot
  139. Warm
  140. Tepid
  141. Cold  
  142. 4
  143. According to my detergent bottle, cold is best for colors that might run.
  144. 5
Tags: python student
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement