Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Quizzes.
- easy_quiz = "'Portugal is a country in ___1___. Portugal is bordered to the \
- east by ___2___, and to the west by the ___3___ Ocean. ___4___ is the primary language \
- spoken in Portugal.'"
- medium_quiz = "'Tanzania is a country in ___1___. Tanzania is popular for \
- travelers interested in climbing Mount ___2___ or going on safari in the \
- ___3___ National Park. The island of ___4___ lies just to the east of Tanzania \
- and is popular for its beaches and marine life.'"
- hard_quiz = "'Thailand is a country in Southeast ___1___. A popular city in \
- northern Thailand is ___2___. Thailand has many ___3___ off its southern coast, \
- and is bordered to the south by ___4___, another beautiful country.'"
- # Answers to the quizzes.
- easy_answers = ["europe", "spain", "atlantic", "portuguese"]
- medium_answers = ["africa", "kilimanjaro", "serengeti", "zanzibar"]
- hard_answers = ["asia", "chiang mai", "islands", "malaysia"]
- # A list of the blanks to be replaced by the player's guesses.
- blanks = ["___1___", "___2___", "___3___", "___4___"]
- # Introduction text will print when the game first opens.
- print "Welcome to the Fill-in-the-Blanks Quiz!"
- # Main contents of game. The function will find each blank in the quiz and then ask
- # the player to enter an answer. Players have three attempts to answer each question.
- # If they answer incorrectly (if their answer doesn't match the answers list), they will
- # be prompted to try again via the incorrect_answer function. If they answer correctly,
- # the function will run the replace_blanks function, which will return the quiz with that
- # blank (and all previous blanks) filled in. The player will then be asked the next question.
- def play_game(quiz, answers):
- index, attempts_left = 0, 3
- max_index, min_attempts_left = 3, 1
- quiz = quiz.split()
- while index <= max_index and attempts_left >= min_attempts_left:
- for word in quiz:
- if word in blanks:
- user_guess = raw_input("Fill in number " + str(index + 1) + ": ").lower()
- if user_guess != answers[index]:
- attempts_left -= 1
- incorrect_answer(attempts_left, min_attempts_left)
- if attempts_left < min_attempts_left:
- break
- else:
- quiz = replace_blanks(quiz, blanks, index, user_guess)
- index += 1
- attempts_left = 3
- winning_quiz(index, max_index)
- # If user answers question correctly, this function will replace the blank with the correct
- # answer and print the quiz with the replacement and all previous blanks filled in.
- def replace_blanks(quiz, blanks, index, user_guess):
- quiz = " ".join(quiz)
- quiz = quiz.replace(blanks[index], user_guess)
- print "Correct!" + "\n" + quiz
- quiz = quiz.split()
- return quiz
- # Tells the user they won and ends quiz when the last question has been answered correctly
- # and the quiz has passed the max_index value of 3.
- def winning_quiz(index, max_index):
- if index > max_index:
- print "Correct. You won!"
- # Responds to the user if they answer a question incorrectly by prompting them for another answer.
- # If they are out of attempts, which will happen when the min_attempts_left value of 0
- # is reached, this function will tell them they lost.
- def incorrect_answer(attempts_left, min_attempts_left):
- if attempts_left < min_attempts_left:
- print "That's not correct. Sorry - you lose!"
- else:
- print "That's not correct! You have " + str(attempts_left) + " tries left!"
- # Asks player to select level of difficulty. If they don't answer with either
- # easy, medium, or hard, it will ask them again. Once they correctly select a level
- # of difficulty, this function will then tell them they have three attempts to fill in
- # each blank and tell them to begin playing. It then prints out the quiz they chose
- # and returns the play_game function.
- def select_difficulty():
- chosen_level = raw_input("Please select your level of difficulty: easy, medium, or hard: ").lower()
- if chosen_level == "easy":
- print "You've selected the easy quiz! You have 3 attempts to fill in each blank. Let's begin!"
- print easy_quiz
- return play_game(easy_quiz, easy_answers)
- elif chosen_level == "medium":
- print "You've selected the medium quiz! You have 3 attempts to fill in each blank. Let's begin!"
- print medium_quiz
- return play_game(medium_quiz, medium_answers)
- elif chosen_level == "hard":
- print "You've selected the hard quiz! You have 3 attempts to fill in each blank. Let's begin!"
- print hard_quiz
- return play_game(hard_quiz, hard_answers)
- else:
- print "I didn't understand your answer."
- select_difficulty()
- select_difficulty()
Add Comment
Please, Sign In to add comment