Nested If Expression with user validation - repeating the prompt for user input
谢谢你@idor我正在取得一些进展,但我还没有100%的进步。现在,我的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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) #time.sleep(1) print"Ok, lets see your results. Does it make sense?" #time.sleep(1) return replaced #time.sleep(1) |
打印简易游戏(简易文本,部分语言1)
你可以看到我添加了
- 当你输入正确的答案时,程序继续回答问题2,同时将i增加1。
- 如果输入正确,这将从头到尾工作。
- 当您输入错误的答案时,系统会提示您再次输入。好!
- 然而,用户会陷入这个问题,尽管我已经增加到正确的值。
我真的不明白为什么当我被增加的时候,用户会被困在这一点上,也就是说,我们会在列表中的正确位置检查正确答案。
这是游戏的完整代码。我可以在我的Mac上成功运行它,但是可以看到上面的行为。有什么想法吗?事先谢谢!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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) #time.sleep(1) print"Ok, lets see your results. Does it make sense?" #time.sleep(1) return replaced #time.sleep(1) print easy_game(easy_text, parts_of_speech1)<wyn> |
代码>
我正在使用几个不同的列表操作,根据原始输入构建一个测验。我还想在继续进行测验中的下一个问题之前,根据列表验证用户输入。
函数当前如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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行中,我对照包含解决方案单词的列表进行检查。虽然验证本身可以工作,但是函数只继续下一个问题,但我需要它重复这个问题,直到得到正确的答案。我试着重新定位不同的线条,但在这一点上我根本就没法在周围转悠。我需要在哪里或如何正确地放置用户输入的验证,以再次提示用户相同的问题?
在我看来,你要找的是一个
而不是:
1 2 | if word != solution_list1[0]: print"Sorry, you are wrong. Try again!" |
尝试:
1 2 3 4 | 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) |
这样,用户必须再次回答问题(