Make this code better

This is a quiz

A sports quiz with multiple answers with difficulty levels, the code is not working properly

SportsQuiz
# My Sports Quiz # Global Variable blanks = ["___1___", "___2___", "___3___", "___4___"] easy_paragraph = ''' The Dallas Cowboys are a ___1___ team.The Cleveland Cavliers are a ___2___ team. The Atlanta Braves are a ___3___ team. The Boston Bruins are a ___4___ team. ''' medium_paragraph = '''The ___1___ won the 2017 NFL Championship. The 2017 NBA Championship was won by ___2___.The ___3___ were the 2016 MLB champions and the ___4____ won the 2017 Hockey championship. ''' hard_paragraph = ''' The NFL champion is awarded the ___1___ trophy. The award for basketball is named___2___ trophy. Baseball named its award___3___ trophy. Hockeys trophy is named___4___ cup.''' easy_answers = ["football", "basketball", "baseball", "hockey"] medium_answers = ["patriots", "warriors", "cubs", "penguins"] hard_answers = ["vince lombardi", "larry o'brien", "commissioners", "stanley"] # Helper Functions def greeting(player_name): """To display initial player and game information. Inputs: Inputted name of player. Outputs: Introduction to game greeting.""" print "\nGreat! Welcome, " + player_name + ". The purpose of this game is to fill in the blanks for all the sentences provided. Now, please pick a level by entering in the appropriate number: " def get_setup(level): """To group all the paragraphs and answers together. Inputs: Current level. Outputs: The specific paragraph and its answers associated with that level.""" if level == "1": print "\nYou chose level 1 - Easy.\n" return easy_paragraph, easy_answers elif level == "2": print "\nYou chose level 2 - Medium.\n" return medium_paragraph, medium_answers else: # For level 3. print "\nYou chose level 3 - Hard.\n" return hard_paragraph, hard_answers def word_in_blanks(word, blanks): """To single out every blank. Inputs: 'word' is the each blank in the 'blanks' list. 'blanks' is the list. Outputs: Returns 'word' if that matches up with the current word in the paragraph.""" for blank in blanks: # For every blank in the blanks array. if blank in word: # If blank is in answer. return blank return None def replace_the_blank(word, replaced, blanks, user_answer, index): """To replace each blank with its correct answer. Part 2. Inputs: 'blanks' list, the replaced paragraph that has the correct answers so far, if applicable, the 'user_answer' for that blank, the index number of that 'user_answer' to correctly match that with the right blank to fill in. Outputs: The correctly replaced paragraph.""" if word_in_blanks(word, blanks) == None: if word not in replaced: replaced.append(word) else: replacement = word_in_blanks(word, blanks) word = word.replace(replacement, user_answer.upper()) if replacement == blanks[index]: if replacement not in replaced: replaced.append(word) else: position = replaced.index(replacement) replaced[position] = word else: replaced.append(replacement) return replaced def fill_in_answers(paragraph, blanks, replaced, user_answer, index): """To replace each blank with its correct answer. Part 1. Inputs: Paragraph from level, 'blanks' list, the replaced paragraph that has the correct answers so far, if applicable, the 'user_answer' for that blank, the index number of that 'user_answer' to correctly match that with the right blank to fill in. Outputs: The correctly replaced paragraph.""" split_paragraph = paragraph.split() if type(replaced) == str: replaced = replaced.split() for word in split_paragraph: replace_the_blank(word, replaced, blanks, user_answer, index) replaced = " ".join(replaced) head, sep, tail = replaced.partition("Sports") # To get rid of the extra 'replacements' that tacked on to the end of every paragraph. Inputs: The replaced paragraph. Outputs: Cleaned replaced paragraph. replaced = head + sep return replaced def collect_answers(level, paragraph, answers): """To collect the user's answers. Inputs: The current level, its paragraph, and its answers. Outputs: The updated replaced paragraph, the index of each answer.""" replaced = [] user_answer = "" index = 0 for blank in blanks: # Where the questions and answers gets stated. question = "\nWhat is your answer for " + blank + "?" print question user_answer = raw_input("Type here: ") user_answer = user_answer.lower() while user_answer != answers[index]: print "\nYour answer was wrong. Please try again.\n" user_answer = raw_input("Type it here again: ") user_answer = user_answer.lower() print "\nAwesome, that's correct!\n" replaced = fill_in_answers(paragraph, blanks, replaced, user_answer, index) print replaced index += 1 return replaced, index # Starting to actually play the 'game'... def play_game(): """To execute the main program/game. Inputs: None. Outputs: Whole program.""" player_name = raw_input("What is the name of the current player? ") greeting(player_name) level = raw_input("\nEasy - 1 | Medium - 2 | Hard - 3 | ") if level == "1" or level == "2" or level == "3": paragraph, answers = get_setup(level) print paragraph replaced = collect_answers(level, paragraph, answers) print "\nYAY, " + player_name + ", YOU WON THE GAME!\n" else: print "\nWRONG! Please pick an actual level, " + player_name + ". Game will now restart.\n" play_game() play_game()

Reactions 0 reactions

No reactions yet, be the first: comment or tweak the code

Comment on this code:

Please Login or Register to leave a comment.