list - Unexpected output from append within loop (Python) -
right sorry, heres code
level_1 = "the _1_ man in _2_ place can make _3_ in _4_" level_2 = "we both _1_ lot of _2_ you're going _3_. _4_ \ can put our _5_ behind us. science. " \ "you _6_ " level_3 = "everyone have _1_ has either _2_ or left me._3_ f***ing except \ _4_! don't _5_ me i'd _6_ " \ "with else, because _7_ more _8_ " blank_words = ["_1_", "_2_", "_3_", "_4_", "_5_", "_6_", "_7_", "_8_", ] level_1_answers = ["right", "wrong", "difference", "world"] level_2_answers = ["said", "things", "regret", "think", "differences", "monster"] level_3_answers = ["cared", "died", "everyone", "you", "tell", "safer", "truth", "scared"] def level_selector(): """gets input user see level game starts on""" while true: try: level = int(raw_input("there 3 levels, pick start \n -->")) except valueerror: print "please select number" else: if 1 <= level <= 3: if level == 1: return level_1, level_1_answers, level elif level == 2: return level_2, level_2_answers, level elif level == 3: return level_3, level_3_answers, level else: print "select number within level range" def blanks_to_fill(word, blanks): blank in blanks: if blank in word: return blank return none def fill_blanks(word, replaced, blanks, user_input, index): if blanks_to_fill(word, blanks) none: if word not in replaced: replaced.append(word) else: replacement = blanks_to_fill(word, blanks) word = word.replace(replacement, user_input.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 format_answers(quote, blanks, replaced, user_input, index): split_quote = quote.split() if type(replaced) == str: replaced = replaced.split() word in split_quote: fill_blanks(word, replaced, blanks, user_input, index) replaced = " ".join(replaced) return replaced def user_answer(level, quote, answers): replaced = [] user_input = "" index = 0 blank_range = len(answers) blank in blank_words[:blank_range]: prompt = "fill blank " + blank print prompt user_input = raw_input("-->") user_input = user_input.lower() while user_input != answers[index]: print "incorrect, please try again." user_input = raw_input("-->") user_input = user_input.lower() print "good job!" replaced = format_answers(quote, blank_words, replaced, user_input, index) print replaced index += 1 return replaced, index def play_again(): user_response = raw_input("would play again, yes or no?").lower() if user_response.startswith("y"): play() else: pass def play(): quote, answers, level = level_selector() print quote replaced = user_answer(level, quote, answers) print "well done, finished level" play_again() play()
the output i'm meant quote selected level correct response player. instead i'm getting output http://i.imgur.com/esaofbb.png words missing , "blank" placeholders being tagged on end of output. can see problem either in "fill_blanks" or "format_answers" functions can't see myself.
perhaps fresh pair of eyes can shed light.
greatly appreciated if respond, i've tried part myself, hit brick wall
Comments
Post a Comment