2017-08-27 45 views
1

所以標題可能沒有意義。但這裏是代碼:將某些內容添加到字符串後,如何打印修改以及字符串的其餘部分?

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行是我遇到的麻煩。感謝您的任何建議。

+0

一個正則表達式可能會工作,但你有種做這是一個過於複雜的方式。我可能會單獨存儲完整的字符串,然後只在需要打印時才添加「下劃線填充符」。我可以看到,比試圖替換現有字符串的位更容易。 – Carcigenicate

+0

@Carcigenicate這很有趣。我將盡力實現 – louielouielouie

+0

這樣,一旦用戶獲得了正確的答案,您可以用正確的答案替換填充程序。我想盡量詳細說明,但我剛剛離開夜班,感到腦死亡。祝你好運。 – Carcigenicate

回答

2

您可以使用string formatting創建與佔位符(replacement_field)的字符串,用一些預定義的變量填充,因爲用戶只需更改變量即可。格式規範允許指定的佔位符

s = "The movie {ans1} is a war movie directed by {ans2} Nolan about the {ans3} and French armies stranded on the {ans4} of Dunkirk while the {ans5} army closed in on them." 

這使得它方便地在佔位符填充字典

d = {'ans1' : '__1__', 'ans2' : '__2__', 
    'ans3' : '__3__', 'ans4' : '__4__', 
    'ans5' : '__5__'} 

你使用這樣的:

>>> s.format(**d) 
'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.' 

改變這樣的答案

>>> d['ans1'] = 'Ziegfield Follies' 
>>> s.format(**d) 
'The movie Ziegfield Follies 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.' 
>>> 
+0

哦,這也是一個很好的想法。可能比我的建議更Pythonic。 – Carcigenicate

+0

這真的很有趣。我還沒有學到甚至沒有看到任何python字典的例子。它看起來比基本清單更有效率,就像我正在嘗試的那樣。謝謝你的回答 – louielouielouie

+0

@Carcigenicate我在寫這篇文章的時候閱讀了你的評論,這些想法基本上是一樣的,我差點給你提一提。 – wwii

0

假設你使用最新的Python來學習(3.6),你可以使用f-字符串。大括號中的項目可以是大多數Python表達式。在這種情況下,他們的索引單詞列表:

import textwrap 

def paragraph(words): 
    s = f'The movie {words[0]} is a war movie directed by {words[1]} Nolan about the {words[2]} and French armies stranded on the {words[3]} of Dunkirk while the {words[4]} army closed in on them.' 
    print() 
    print(textwrap.fill(s)) 

words = '__1__ __2__ __3__ __4__ __5__'.split() 
paragraph(words) 
words[0] = 'Dunkirk' 
paragraph(words) 

輸出:

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. 

The movie Dunkirk 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. 
相關問題