Advertisement
Dayeeta

Ch 7 2

Nov 16th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #Hello,this is Dayeeta Das,NTU ID:N0830182#
  2. #This is the The Chapter 7 Challenge 1
  3. #This programe demonstrates the Trivia game challenge in Python.In this game a quiz is played.
  4. #If the player scores points>10 then his/her name is stored in the high_score.txt file.
  5. #At the end of every question the player gets to know if the answer they guessed is right/wrong
  6. #The player's total score is displayed at the end of the game
  7. #Importing the sys function
  8. import sys
  9. #Defining a functions for various operations like opening the file,moving to the next line
  10. #to get the questions,storig the answers in a list and displaying the total score
  11. def open_file(file_name, mode):
  12. """Open a file."""
  13. try:
  14. the_file = open(file_name, mode)
  15. except IOError as e:
  16. print("Unable to open the file", file_name, "Ending program.\n", e)
  17. input("\n\nPress the enter key to exit.")
  18. sys.exit()
  19. else:
  20. return the_file
  21.  
  22. def next_line(the_file):
  23. """Return next line from the trivia file, formatted."""
  24. line = the_file.readline()
  25. line = line.replace("/", "\n")
  26. return line
  27.  
  28. def next_block(the_file):
  29. """Return the next block of data from the trivia file."""
  30. category = next_line(the_file)
  31.  
  32. question = next_line(the_file)
  33.  
  34. answers = []
  35. for i in range(4):
  36. answers.append(next_line(the_file))
  37.  
  38. correct = next_line(the_file)
  39. if correct:
  40. correct = correct[0]
  41.  
  42. points = next_line(the_file)
  43. if points:
  44. points = points[0]
  45.  
  46. explanation = next_line(the_file)
  47.  
  48. return category, question, answers, correct, points, explanation
  49.  
  50. def welcome(title):
  51. """Welcome the player and get his/her name."""
  52. print("\t\tWelcome to Trivia Challenge!\n")
  53. print("\t\t", title, "\n")
  54. def high_scores():
  55. global score
  56. value=int(score)
  57. name=input("What is your name:")
  58. entry=(value,name)
  59. f=open("hidh_scores.dat","wb")
  60. high_scores=pickle.load(f)
  61. high_scores.append(entry)
  62. high_scores=high_scores[:5]
  63. print("High Scores")
  64. print("Name \t Score")
  65. for entry in high_scores:
  66. value,name=entry
  67. print(name,"\t",value)
  68. pickle.dump(high_scores,f)
  69. f.close()
  70.  
  71. def main():
  72. trivia_file = open_file("trivia.txt", "r")
  73. title = next_line(trivia_file)
  74. welcome(title)
  75. score = 0
  76.  
  77. # get first block
  78. category, question, answers, correct, points, explanation = next_block(trivia_file)
  79. while category:
  80. # ask a question
  81. print(category)
  82. print(question)
  83. for i in range(4):
  84. print("\t", i + 1, "-", answers[i])
  85.  
  86. # get answer
  87. answer = input("What's your answer?: ")
  88.  
  89. # check answer
  90. if answer == correct:
  91. print("\nRight!", end=" ")
  92. score += int(points)
  93. else:
  94. print("\nWrong.", end=" ")
  95. print(explanation)
  96. print("Score:", score, "\n\n")
  97.  
  98. # get next block
  99. category, question, answers, correct, points, explanation = next_block(trivia_file)
  100.  
  101. trivia_file.close()
  102.  
  103. print("That was the last question!")
  104. print("You're final score is", score)
  105. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement