Guest User

Dawson Chapter 7

a guest
Jan 21st, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1.  
  2. def next_line(the_file):
  3.     """Return next line from the trivia file, formatted."""
  4.     line = the_file.readline()
  5.     line = line.replace("/", "\n")
  6.     return line
  7.  
  8. def next_block(the_file):
  9.     """Return the next block of data from the trivia file."""
  10.     category = next_line(the_file)
  11.    
  12.     question = next_line(the_file)
  13.    
  14.     answers = []
  15.     for i in range(4):
  16.         answers.append(next_line(the_file))
  17.        
  18.     correct = next_line(the_file)
  19.     if correct:
  20.         correct = correct[0]
  21.        
  22.     explanation = next_line(the_file)
  23.  
  24.     return category, question, answers, correct, explanation
  25.  
  26. def welcome(title):
  27.     """Welcome the player and get his/her name."""
  28.     print("\t\tWelcome to Trivia Challenge!\n")
  29.     print("\t\t", title, "\n")
  30.  
  31. def main():
  32.     trivia_file = open_file("trivia.txt", "r")
  33.     title = next_line(trivia_file)
  34.     welcome(title)
  35.     score = 0
  36.  
  37.     # get first block
  38.     category, question, answers, correct, explanation = next_block(trivia_file)
  39.     while category:
  40.         # ask a question
  41.         print(category)
  42.         print(question)
  43.         for i in range(4):
  44.             print("\t", i + 1, "-", answers[i])
  45.  
  46.         # get answer
  47.         answer = input("What's your answer?: ")
  48.  
  49.         # check answer
  50.         if answer == correct:
  51.             print("\nRight!", end=" ")
  52.             score += 1
  53.         else:
  54.             print("\nWrong.", end=" ")
  55.         print(explanation)
  56.         print("Score:", score, "\n\n")
  57.  
  58.         # get next block
  59.         category, question, answers, correct, explanation = next_block(trivia_file)
  60.  
  61.     trivia_file.close()
  62.  
  63.     print("That was the last question!")
  64.     print("You're final score is", score)
  65.  
  66. main()  
  67. input("\n\nPress the enter key to exit.")
Advertisement
Add Comment
Please, Sign In to add comment