Guest User

Untitled

a guest
Mar 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. # Quizzes.
  2. easy_quiz = "'Portugal is a country in ___1___. Portugal is bordered to the \
  3. east by ___2___, and to the west by the ___3___ Ocean. ___4___ is the primary language \
  4. spoken in Portugal.'"
  5. medium_quiz = "'Tanzania is a country in ___1___. Tanzania is popular for \
  6. travelers interested in climbing Mount ___2___ or going on safari in the \
  7. ___3___ National Park. The island of ___4___ lies just to the east of Tanzania \
  8. and is popular for its beaches and marine life.'"
  9. hard_quiz = "'Thailand is a country in Southeast ___1___. A popular city in \
  10. northern Thailand is ___2___. Thailand has many ___3___ off its southern coast, \
  11. and is bordered to the south by ___4___, another beautiful country.'"
  12.  
  13. # Answers to the quizzes.
  14. easy_answers = ["europe", "spain", "atlantic", "portuguese"]
  15. medium_answers = ["africa", "kilimanjaro", "serengeti", "zanzibar"]
  16. hard_answers = ["asia", "chiang mai", "islands", "malaysia"]
  17.  
  18. # A list of the blanks to be replaced by the player's guesses.
  19. blanks = ["___1___", "___2___", "___3___", "___4___"]
  20.  
  21. # Introduction text will print when the game first opens.
  22. print "Welcome to the Fill-in-the-Blanks Quiz!"
  23.  
  24. # Main contents of game. The function will find each blank in the quiz and then ask
  25. # the player to enter an answer. Players have three attempts to answer each question.
  26. # If they answer incorrectly (if their answer doesn't match the answers list), they will
  27. # be prompted to try again via the incorrect_answer function. If they answer correctly,
  28. # the function will run the replace_blanks function, which will return the quiz with that
  29. # blank (and all previous blanks) filled in. The player will then be asked the next question.
  30. def play_game(quiz, answers):
  31. index, attempts_left = 0, 3
  32. max_index, min_attempts_left = 3, 1
  33. quiz = quiz.split()
  34. while index <= max_index and attempts_left >= min_attempts_left:
  35. for word in quiz:
  36. if word in blanks:
  37. user_guess = raw_input("Fill in number " + str(index + 1) + ": ").lower()
  38. if user_guess != answers[index]:
  39. attempts_left -= 1
  40. incorrect_answer(attempts_left, min_attempts_left)
  41. if attempts_left < min_attempts_left:
  42. break
  43. else:
  44. quiz = replace_blanks(quiz, blanks, index, user_guess)
  45. index += 1
  46. attempts_left = 3
  47. winning_quiz(index, max_index)
  48.  
  49. # If user answers question correctly, this function will replace the blank with the correct
  50. # answer and print the quiz with the replacement and all previous blanks filled in.
  51. def replace_blanks(quiz, blanks, index, user_guess):
  52. quiz = " ".join(quiz)
  53. quiz = quiz.replace(blanks[index], user_guess)
  54. print "Correct!" + "\n" + quiz
  55. quiz = quiz.split()
  56. return quiz
  57.  
  58. # Tells the user they won and ends quiz when the last question has been answered correctly
  59. # and the quiz has passed the max_index value of 3.
  60. def winning_quiz(index, max_index):
  61. if index > max_index:
  62. print "Correct. You won!"
  63.  
  64. # Responds to the user if they answer a question incorrectly by prompting them for another answer.
  65. # If they are out of attempts, which will happen when the min_attempts_left value of 0
  66. # is reached, this function will tell them they lost.
  67. def incorrect_answer(attempts_left, min_attempts_left):
  68. if attempts_left < min_attempts_left:
  69. print "That's not correct. Sorry - you lose!"
  70. else:
  71. print "That's not correct! You have " + str(attempts_left) + " tries left!"
  72.  
  73. # Asks player to select level of difficulty. If they don't answer with either
  74. # easy, medium, or hard, it will ask them again. Once they correctly select a level
  75. # of difficulty, this function will then tell them they have three attempts to fill in
  76. # each blank and tell them to begin playing. It then prints out the quiz they chose
  77. # and returns the play_game function.
  78. def select_difficulty():
  79. chosen_level = raw_input("Please select your level of difficulty: easy, medium, or hard: ").lower()
  80. if chosen_level == "easy":
  81. print "You've selected the easy quiz! You have 3 attempts to fill in each blank. Let's begin!"
  82. print easy_quiz
  83. return play_game(easy_quiz, easy_answers)
  84. elif chosen_level == "medium":
  85. print "You've selected the medium quiz! You have 3 attempts to fill in each blank. Let's begin!"
  86. print medium_quiz
  87. return play_game(medium_quiz, medium_answers)
  88. elif chosen_level == "hard":
  89. print "You've selected the hard quiz! You have 3 attempts to fill in each blank. Let's begin!"
  90. print hard_quiz
  91. return play_game(hard_quiz, hard_answers)
  92. else:
  93. print "I didn't understand your answer."
  94. select_difficulty()
  95. select_difficulty()
Add Comment
Please, Sign In to add comment