Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在向字符串添加内容后,如何打印修改内容以及字符串的其余部分?_Python - Fatal编程技术网

Python 在向字符串添加内容后,如何打印修改内容以及字符串的其余部分?

Python 在向字符串添加内容后,如何打印修改内容以及字符串的其余部分?,python,Python,所以这个标题可能没有意义。但代码如下: 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_

所以这个标题可能没有意义。但代码如下:

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_字符串:

"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的初学者,但我一直在为列表和索引值而挣扎。如果您想查看全部内容:

第55行是我遇到的问题。感谢您提供的任何建议。

您可以使用占位符(替换字段)创建字符串,占位符将填充一些预定义的变量,当用户回答时,您只需更改变量即可。格式规范允许命名占位符

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.'
>>>

假设您使用的是最新的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.

正则表达式可能会起作用,但你这样做是一种过于复杂的方式。我可能会单独存储完整的字符串,然后只在需要打印时添加“下划线填充符”。我可以看出,这比替换现有字符串的位容易得多。@Carcigenicate这很有趣。我将尝试以这种方式实现,一旦用户得到正确的答案,您就可以用正确的答案替换填充符。我想详细解释一下,但我刚下了夜班,感觉脑死亡。祝你好运。如果Python有一个用于列表的“interleave”函数,你可以只
交错
完整的字符串和填充符/答案,然后
使用空字符串连接独立的字符串。然后,只要在用户正确回答时替换填充/回答列表中的全部内容即可。@Carcigenicate谢谢您的帮助,我非常感谢。你也帮我解决了我的最后一个问题。哦,这也是个好主意。可能比我的建议更像蟒蛇。那真的很有趣。我还没有学过甚至没有见过任何python词典的例子。它看起来比基本列表更有效,就像我正在尝试的那样。谢谢你的支持answer@Carcigenicate我在写这篇文章的时候读到了你的评论,这些想法基本上和我差点提到你的想法一样。@LouieLouie-你应该花点时间来研究一下,你会对Python提供的帮助你解决问题的工具有更好的了解。@wwii我的Python生锈了,但这看起来像字符串替换,而我建议将填充符与字符串交错。但是应该有相同的结果。