嵌套If Expression with user validation - 重复提示用户输入

时间:2016-01-08 19:21:54

标签: python python-2.7 validation if-statement for-loop

谢谢@Idor我正在取得一些进展,但我还不是100%。现在我的代码如下:

def easy_game(easy_text, parts_of_speech1):    
replaced = []
easy_text = easy_text.split()
i = 0
for word in easy_text:
    replacement = word_in_pos_easy(word, parts_of_speech1)
    if replacement != None:
        user_input = raw_input("Type in: " + replacement + " ")
        word = word.replace(replacement, user_input)
        while word != solutions[i]:    
            print "Sorry, you are wrong"
            user_input = raw_input("Type in: " + replacement + " ")
            print i
        i = i + 1
        print i
        replaced.append(word)
    else:
        replaced.append(word)
replaced = " ".join(replaced)
print
#time.sleep(1)
print "Ok, lets see your results. Does it make sense?"
print
#time.sleep(1)
return replaced
print  
#time.sleep(1) 

print easy_game(easy_text,parts_of_speech1)

你可以看到我添加了while循环。我还添加了一个索引,为了排除故障,我添加了print i来查看程序正在执行的操作。它仍然让我感到困惑或者没有像我期望的那样工作。但作为编程我的期望的新手可能是错误的。这是正在发生的事情:

  • 当您输入正确答案时,程序继续问题2并且还将i增加1
  • 如果您正确输入内容,这将从头到尾工作
  • 当您输入错误答案时,系统会提示您再次输入。好!
  • 然而,尽管我已经增加到了正确的价值,但用户却陷入了这个问题。

我真的不明白为什么当我增加时用户会被卡住,即我们会检查列表中的正确位置以获得正确答案。

这是游戏的完整代码。我可以在我的Mac上成功运行它,但看到上面的行为。有什么想法吗?提前谢谢!

parts_of_speech1  = ["Word1", "Word2", "Word3", "Word4"]
        # The following is the text for the easy text..
easy_text = "Python is a Word1 language that provides constructs intended to enable clear programs on both small and large scale. Python implementation was started in December Word2 by Guido von Rossum. The most simple Word3 in Python is Word4 and normally used at the beginning to tell Python to write 'Hello World' on the screen."
solutions = ["programming", "1989", "function", "print"]
        # Checks if a word in parts_of_speech is a substring of the word passed in.
def word_in_pos_easy(word, parts_of_speech1):
            for pos in parts_of_speech1:
                if pos in word:
                    return pos
            return None 
        # Plays a full game of mad_libs. A player is prompted to replace words in the easy text, 
        # which appear in parts_of_speech with their own words.
def easy_game(easy_text, parts_of_speech1):
replaced = [] easy_text = easy_text.split() i = 0 for word in easy_text: replacement = word_in_pos_easy(word, parts_of_speech1) if replacement != None: user_input = raw_input("Type in: " + replacement + " ") word = word.replace(replacement, user_input) while word != solutions[i]:
print "Sorry, you are wrong" user_input = raw_input("Type in: " + replacement + " ") print i i = i + 1 print i replaced.append(word) else: replaced.append(word) replaced = " ".join(replaced) print #time.sleep(1) print "Ok, lets see your results. Does it make sense?" print #time.sleep(1) return replaced print
#time.sleep(1) print easy_game(easy_text, parts_of_speech1)

我正在使用几个不同的列表操作构建基于raw_input的测验。我还希望在进入测验中的下一个问题之前根据列表验证用户输入。

该功能目前如下所示:

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

在第9行中,我正在检查包含解决方案单词的List。虽然验证本身起作用,但函数只是继续下一个问题,但我需要它重复这个问题,直到得到正确的答案。我试图重新定位不同的线条,但此时根本无法绕过它。我需要在何处或如何正确地验证用户输入以再次提示用户输入相同的问题?

1 个答案:

答案 0 :(得分:1)

在我看来,你正在寻找的是一个while循环。

而不是:

if word != solution_list1[0]:
    print "Sorry, you are wrong. Try again!"

尝试:

while word != solution_list1[0]:
    print "Sorry, you are wrong. Try again!"
    user_input = raw_input("Type in a: " + replacement + " ") # ask the user again
    word = word.replace(replacement, user_input)

这样,用户必须再次回答问题(raw_input),直到他做对了。