Sep-14-2017, 02:58 PM
NOOB to Python
I need some help to reduce the size of my code by adding a loop which iterates on the blanks- I have been working on this code for weeks and just don't understand how to perform this task. Could someone please edit this code and assist me with my pain? Thank you.
Let me explain what is require to be done. You have written 4 functions for each blank. In each function, you will see a repetition of the code snippet. Instead of functions sequentially, add a loop which iterates on blanks in the paragraph. This is not only necessary because the rubric says "Functions are used as tools to automate tasks which are likely to be repeated" but you will also see how many numbers of lines are reduced. Functions are really powerful and an example given below will help you to understand:
I need some help to reduce the size of my code by adding a loop which iterates on the blanks- I have been working on this code for weeks and just don't understand how to perform this task. Could someone please edit this code and assist me with my pain? Thank you.
Let me explain what is require to be done. You have written 4 functions for each blank. In each function, you will see a repetition of the code snippet. Instead of functions sequentially, add a loop which iterates on blanks in the paragraph. This is not only necessary because the rubric says "Functions are used as tools to automate tasks which are likely to be repeated" but you will also see how many numbers of lines are reduced. Functions are really powerful and an example given below will help you to understand:
# Welcome to the Mad Libs generator for Project 2. # This is a sports quiz to test your knowledge # This is the easy level fill in the blank sports quiz. easy_level_string="The NFL consists of __1__ football teams.\n" \ "There are __2__ players are allowed on each active roster. \n" \ "The defending Super Bowl Champion is the __3__.\n" \ "The 2016 NFL MVP was __4__." # This is the medium level fill in the blank sports quiz. medium_level_string="California has __1__ Major League Baseball teams.\n" \ "The last team to win the World Series from California was the __2__.\n" \ "The 2014 World Series MVP was __3__.\n" \ "__4__ games were played in the 2014 World Series." # This is the hard level fill in the blank sports quiz. hard_level_string="Roger __1__ holds the record for mens tennis Grand Slam titles.\n" \ "He has won __2__ major titles.\n" \ "__3__ is the coutry that this champion is from.\n" \ "He has won the __4__ championship 8 times." # Answers to the easy, medium, and hard sports quiz easy_level_answers=['32', '53', 'new england patriots','matt ryan'] medium_level_answers=['5', 'san francisco giants', 'madison bumgarner','7'] hard_level_answers=['federer', '19', 'switzerland','wimbledon'] # Player chooses difficulty level of quiz. def choose_level(): print "Please choose your difficulty level for this sports game!" level_name = raw_input("Please type in easy, medium or hard. \n") print "All answers should be in lower case only!" if level_name=="easy": print get_level(level_name) elif level_name=="medium": get_level(level_name) elif level_name=="hard": get_level(level_name) else: print "I'm sorry but that is not a valid selection, try again" return choose_level() # Program executes questions based on players selection of difficulty. def get_level(level_name): if level_name=="easy": print easy_level_string level_answers=easy_level_answers return level_first_blank(easy_level_string,level_answers) elif level_name=="medium": print medium_level_string level_answers=medium_level_answers return level_first_blank(medium_level_string,level_answers) else: print hard_level_string level_answers=hard_level_answers return level_first_blank(hard_level_string,level_answers) # Program evaluates player input - if correctly answered - moves to next question. # If incorrect, retry required. After 3rd incorrect attempt, game is over def level_first_blank(level_string,level_answers): number_of_tries=2 print "All answers should be in lower case only!" user_input=raw_input("Type in your answer for the 1st blank:") if user_input==level_answers[0]: print "Awesome!" first_blank_correct_string=level_string.replace('__1__',user_input) return level_second_blank(first_blank_correct_string,level_answers) else: while number_of_tries>0: print "You have "+str(number_of_tries)+ " tries left" user_input = raw_input("Type in your answer for the 1st blank:") if user_input == level_answers[0]: print "Awesome!" first_blank_correct_string = level_string.replace('__1__', user_input) return level_second_blank(first_blank_correct_string,level_answers) number_of_tries=number_of_tries-1 print "You have lost the game and are not a sports genius. Please play again." # Program evaluates player input - if correctly answered - moves to next question. # If incorrect, retry required. After 3rd incorrect attempt, game is over def level_second_blank(first_blank_correct_string,level_answers): print first_blank_correct_string number_of_tries = 2 user_input = raw_input("Type in your answer for the 2nd blank:") if user_input == level_answers[1]: print "Nice Job!!" second_blank_correct_string = first_blank_correct_string.replace('__2__', user_input) return level_third_blank(second_blank_correct_string,level_answers) else: while number_of_tries > 0: print "You have " + str(number_of_tries) + " tries left" user_input = raw_input("Type in your answer for the 2nd blank:") if user_input == level_answers[1]: print "Nice Job!!" second_blank_correct_string = first_blank_correct_string.replace('__2__', user_input) return level_third_blank(second_blank_correct_string,level_answers) number_of_tries = number_of_tries - 1 print "You have lost the game and are not a sports genius. Please play again." # Program evaluates player input - if correctly answered - moves to next question. # If incorrect, retry required. After 3rd incorrect attempt, game is over. def level_third_blank(second_blank_correct_string,level_answers): print second_blank_correct_string number_of_tries = 2 user_input = raw_input("Type in your answer for the 3rd blank:") if user_input == level_answers[2]: print "Excellant!!!" third_blank_correct_string = second_blank_correct_string.replace('__3__', user_input) return level_fourth_blank(third_blank_correct_string,level_answers) else: while number_of_tries > 0: print "You have " + str(number_of_tries) + " tries left" user_input = raw_input("Type in your answer for the 3rd blank:") if user_input == level_answers[2]: print "Excellant!!!" third_blank_correct_string = second_blank_correct_string.replace('__3__', user_input) return level_fourth_blank(third_blank_correct_string,level_answers) number_of_tries = number_of_tries - 1 print "You have lost the game and are not a sports genius. Please play again." # Program evaluates player input - if correctly answered - Successful game completion. # If incorrect, retry required. After 3rd incorrect attempt, game is over. def level_fourth_blank(third_blank_correct_string,level_answers): print third_blank_correct_string number_of_tries = 2 user_input = raw_input("Type in your answer for the 4th blank:") if user_input == level_answers[3]: print "Outstanding!!!!" fourth_blank_correct_string = third_blank_correct_string.replace('__4__', user_input) return level_correct_string(fourth_blank_correct_string) else: while number_of_tries > 0: print "You have " + str(number_of_tries) + " tries left" user_input = raw_input("Type in your answer for the 4th blank:") if user_input == level_answers[3]: print "Outstanding!!!!" fourth_blank_correct_string = third_blank_correct_string.replace('__4__', user_input) return level_correct_string(fourth_blank_correct_string) number_of_tries = number_of_tries - 1 print "You have lost the game and are not a sports genius. Please play again." # Program completetion achieved def level_correct_string(fourth_blank_correct_string): print fourth_blank_correct_string print "Congratulations! You ARE a SPORTS GENIUS!!!!" choose_level()