Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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/0/jpa/2.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 3.x 如何找到字符串中间插入单词_Python 3.x_Caesar Cipher - Fatal编程技术网

Python 3.x 如何找到字符串中间插入单词

Python 3.x 如何找到字符串中间插入单词,python-3.x,caesar-cipher,Python 3.x,Caesar Cipher,我试图写一个凯撒密码,但使它比正常更难。实际的加密是在一个文件上进行的,然后将该文件分成几行。对于每一行,我想在移位之前在开头、中间和结尾添加一个单词。到目前为止,我有这个,但它不起作用: file = str(input("Enter input file:" "")) my_file = open(file, "r") file_contents = my_file.read() #change all "e"s to "zw"s for letter in file_conten

我试图写一个凯撒密码,但使它比正常更难。实际的加密是在一个文件上进行的,然后将该文件分成几行。对于每一行,我想在移位之前在开头、中间和结尾添加一个单词。到目前为止,我有这个,但它不起作用:

file = str(input("Enter input file:" ""))
my_file = open(file, "r")
file_contents = my_file.read()
#change all "e"s to "zw"s 
    for letter in file_contents:
        if letter == "e":
            file_contents = file_contents.replace(letter, "zw")
#add "hokie" to beginning, middle, and end of each line
    lines = file_contents.split('\n')
        def middle_message(lines, position, word_to_insert):
            lines = lines[:position] + word_to_insert + lines[position:]
            return lines
        message = "hokie" + middle_message(lines, len(lines)/2, "'hokie'") + "hokie"
我越来越

TypeError: slice indices must be integers or None or have an __index__ method

我做错了什么?我以为len()返回一个int

假设您正在使用python3

即使修复了len()不是整数的问题,您也需要进行更多的优化

让我们先解决这个问题:我们的示例单词是:

a = 'abcdef'
>>> len(a)
6
>>> len(a) / 2
3.0 #this is your problem, dividing by 2 returns a float number
出现的错误是因为不能将浮点数(3.0和3.5)用作列表(或字符串)中的切片索引,以解决此特定情况下的问题:

>>> len(a) // 2
3
现在,对于优化:

这段代码接受一个文件,我假设它由许多行文本组成,因为您使用“\n”拆分行

当您解决了切片部分时,您将得到另一个错误,告诉您不能将字符串与列表连接起来(此时,您应该使用我上面建议的修复方法尝试您的代码,以便您了解会发生什么)

middle_message函数用于处理单个字符串,但您要将变量“lines”传递给它,它是字符串列表

为了进行测试,我必须通过一个行索引:

message = "hokie" + middle_message(lines[0], len(lines[0])//2, "hokie") + "hokie"
因此,如果您想处理“行”中的所有字符串,列表理解循环可能会很有用,它会为您提供一个带有修改字符串的新列表

newlines = ["hokie" + middle_message(lines[x], len(lines[x])//2, "hokie") + "hokie" for x in range(len(lines))]
我知道,这超过80个字符。。我相信你可以把这个写下来;)

下面是我通过测试得到的结果:

>>> file_contents
'This is going to be my test file\nfor checking some python cipher program.\ni want to see how it works.\n'

>>> lines
['This is going to bzw my tzwst filzw', 'for chzwcking somzw python ciphzwr program.', 'i want to szwzw how it works.', '']

>>> newlines
['hokieThis is going to hokiebzw my tzwst filzwhokie', 'hokiefor chzwcking somzw phokieython ciphzwr program.hokie', 'hokiei want to szwzhokiew how it works.hokie', 'hokiehokiehokie']
在Python3.x中,
len(lines)/2
可能不是整数。。。