所以標題可能沒有意義。但這裏是代碼:將某些內容添加到字符串後,如何打印修改以及字符串的其餘部分?
def play_game(ml_string, blanks, selectedLevel):
replaced = []
ml_string = ml_string.split()
currentQuestion = 0
for blank in ml_string:
replacement = blank_in_quiz(blank, blanks,)
if replacement != None:
user_input = raw_input("Type in the answer for blank " + replacement + " ")
while user_input != allanswers[selectedLevel][currentQuestion]:
print "Incorrect!"
user_input = raw_input("Type in the answer for blank " + replacement + " ")
else:
blank = blank.replace(replacement, user_input)
replaced.append(blank)
print "\nCorrect!\n"
print " ".join(replaced + [currentQuestion,ml_string])
currentQuestion = currentQuestion + 1
else:
replaced.append(blank)
replaced = " ".join(replaced)
print replaced
本質上講這確實是拿這個字符串,這是ml_string:
"The movie __1__ is a war movie directed by __2__ Nolan about the __3__ and French armies stranded on the __4__ of Dunkirk while the __5__ army closed in on them."
而且一旦用戶將正確答案空白,我想打印將答案填在空白處,以及其餘的測驗中還有他們尚未回答的空白。
我是python的初學者,但我一直在努力處理列表和索引值。如果您想查看整體:https://repl.it/KTJh/16
55行是我遇到的麻煩。感謝您的任何建議。
一個正則表達式可能會工作,但你有種做這是一個過於複雜的方式。我可能會單獨存儲完整的字符串,然後只在需要打印時才添加「下劃線填充符」。我可以看到,比試圖替換現有字符串的位更容易。 – Carcigenicate
@Carcigenicate這很有趣。我將盡力實現 – louielouielouie
這樣,一旦用戶獲得了正確的答案,您可以用正確的答案替換填充程序。我想盡量詳細說明,但我剛剛離開夜班,感到腦死亡。祝你好運。 – Carcigenicate