Advertisement
Dayeeta

Ch 7 1

Nov 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 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.  
  55. def main():
  56. trivia_file = open_file("trivia.txt", "r")
  57. title = next_line(trivia_file)
  58. welcome(title)
  59. score = 0
  60.  
  61. # get first block
  62. category, question, answers, correct, points, explanation = next_block(trivia_file)
  63. while category:
  64. # ask a question
  65. print(category)
  66. print(question)
  67. for i in range(4):
  68. print("\t", i + 1, "-", answers[i])
  69.  
  70. # get answer
  71. answer = input("What's your answer?: ")
  72.  
  73. # check answer
  74. if answer == correct:
  75. print("\nRight!", end=" ")
  76. score += int(points)
  77. else:
  78. print("\nWrong.", end=" ")
  79. print(explanation)
  80. print("Score:", score, "\n\n")
  81.  
  82. # get next block
  83. category, question, answers, correct, points, explanation = next_block(trivia_file)
  84.  
  85. trivia_file.close()
  86.  
  87. print("That was the last question!")
  88. print("You're final score is", score)
  89. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement