2017-08-27 80 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.