vendredi 8 janvier 2016

Nested If Expression with user validation - repeating the prompt for user input

I am building out a quiz based on raw_input using several different list operations. I also want to validate the user input against a list before moving on to the next question in the quiz.

The function currently looks like this:

def play_game(ml_string, parts_of_speech):    
replaced = []
ml_string = ml_string.split()
for word in ml_string:
    replacement = word_in_pos(word, parts_of_speech)
    if replacement != None:
        user_input = raw_input("Type in a: " + replacement + " ")
        word = word.replace(replacement, user_input)
        if word != solution_list1[0]:
            print "Sorry, you are wrong. Try again!"
        replaced.append(word)
    else:
        replaced.append(word)
replaced = " ".join(replaced)
return replaced

In Line 9 I am checking against the List containing the solution words. Whereas the validation itself works the function just continues to the next question but I need it to repeat the question until getting the correct answer. I tried to reposition the different lines but simply can't get my head around it at this point in time. Where or how do I need to place the validation of the user input correctly to prompt the user for the same question again?

Aucun commentaire:

Enregistrer un commentaire