SheperdsonBrown SheperdsonBrown - 10 months ago 75
Python Question

Nested functions

I am learning Python and I currently have a function that is long and somewhat repetitive. Please see below: Goal: To reduce the function into parts so I can understand how this process works better.

def play_game(questions,answers):
'''Begins checking user input to answers /
fills in the blanks with correct answer /
prompts user with same question if answer is Wrong
:param questions: feeds .split() list to find __1__
:param answers: searches for answer and replaces blank __1__
:return: replaces correct answer in questions param.
print questions
user_input = raw_input("Fill in the blank: ")
if user_input == answers[0]:
questions = questions.replace('__1__', answers[0])
if user_input != answers[0]:
user_input = raw_input("Wrong answer, you have 4 guesses left. ")
print questions
user_input = raw_input("\n Please answer second question: ")
if user_input == answers[1]:
questions += questions.replace('__2__', answers[1])
if user_input != answers[1]:
user_input = raw_input("\n Incorrect, you have 3 guesses left. ")
print questions

This process will continue for 5 guesses. I want to stress the importance of the same question being asked again if user guesses incorrectly, they will also have a guess reduced from 5 to 4, etc. Should I be using a loop to automate here?

print questions
#for answer in answers:
# process answer
user_input = raw_input("Fill in the blank: ")
if user_input == answers[0]:


try something like this:

for guess in range(4):
    print "Guess", guess + 1

# Put your game function here

    if guess == 3:
        print "Game Over!"