这是我填空的比赛开始的地方。当用户正确回答时,我需要帮助用新句子替换旧句子。我仍然不确定是否应该在一个函数中完成所有操作或获取不同的函数。我的主要功能是“def answer_check(answer,sentence,blank_space):”。我也想使用另外两个函数(如果相关)。“def replace_the_blank(答案,句子,空白):”函数可能需要一些修改才能响应,只有当用户正确猜测并相应地使用下一个字符串时......我不知道怎么回事.....
总的来说,我需要帮助替换来自“driver_Knowledge_test”列表中“blank_space”列表的字符串,并将其替换为“answer_1”列表 - 所有这些都应该在ordert中以及当用户正确时猜测...希望这是有道理的。
driver_knowledge_test = ["When you're going to drive it is important to always put on your ___1___, including your passengers.","If there are no lanes marked on the road, you should drive in the ___2___ side of the road.", "It's getting dark and the sun is fading, you should turn on the ___3___. ","Before driving on a freeway, you should make sure you have enough ___4___, oil, water and the correct tyre pressure."]
answer_1 = ['seatbelts', 'left', 'light', 'fuel']
blank_space = ["___1___", "___2___", "___3___", "___4___"]
'''goal: collect user's answer and replace sentence with word when answered correct - other loop to rettry'''
def finding_blank(word, pos):
for ea_blank in word:
if ea_blank in pos: #This equals to teh whole list.
return ea_blank
return None
def replace_the_blank(answer, sentence, blank):
new_list = []
blank_index = 0
answer_index = 0
sentence_index = 0
for ea_element in sentence:
blank_number = finding_blank(sentence[sentence_index], blank)
if blank_number == blank[blank_index]:
blank_number.replace(blank_index, answer[answer_index])
blank_index += 1
answer_index += 1
sentence_index += 1
"Inputs answer list and sentence string"
"Outputs new sentence when answered correctly"
def answer_check(answer, sentence, blank_space):
new_list = []
index = 0
sentence_index = 0
answer_index = 0
blank_index = 0
while index < sentence:
print sentence[sentence_index]
player_answer = raw_input("\nType your answer for " + blank_space[blank_index] + " \n>>>")
if player_answer.lower() == answer[answer_index]:
new_list.append(sentence[sentence_index])
answer_index += 1
sentence_index += 1
index += 1
blank_index += 1
#print replace_the_blank(answer, sentence, blank)
print "CCORRECT! \n"
else:
print "Wrong! Try again. :) \n"
answer_check(answer_1, driver_knowledge_test, blank_space)
答案 0 :(得分:1)
您可以使用字符串replace方法将字符串的一部分替换为另一个字符串。
所以而不是
print replace_the_blank(answer, sentence, blank)
试
print sentence[sentence_index].replace(blank_space[blank_index], answer[answer_index])