Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/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 - Fatal编程技术网

Python 3.x 简单字扰频器

Python 3.x 简单字扰频器,python-3.x,Python 3.x,程序应该将字符串中的每个单词重新排列成特定的模式,但我不知道如何将加扰后的文本打印到同一行。给你 counter = 0 sentence = 'Hello World' split = str.split(sentence) for str in split: c = split[counter] scramble = c[4] + c[0] + c[3] + c[1] + c[2] counter += 1 print (scramble) 打印功能接受默认为“\n”的e

程序应该将字符串中的每个单词重新排列成特定的模式,但我不知道如何将加扰后的文本打印到同一行。

给你

counter = 0
sentence = 'Hello World'
split = str.split(sentence)

for str in split:
  c = split[counter]
  scramble = c[4] + c[0] + c[3] + c[1] + c[2]
  counter += 1
  print (scramble)

打印功能接受默认为“\n”的
end
参数。将其设置为空字符串可防止它在行尾发出新行。

c=split[counter]
是您的问题
split
已经是一个字符串,因此您将取该字符串的第一个字母,而不是整个单词,并尝试对其进行置乱。您使用
计数器
似乎有点愚蠢,因为您没有使用循环提供的
str
变量。说到它,您不应该使用
str
作为您自己变量的名称,因为它已经是一个内置类型的名称(如果您自己使用它,您将隐藏它)。听起来你可能想用拆分的方式打印单词(单词[4]+单词[0]+单词[3]+单词[1]+单词[2],end=“”)。如果这是正确答案,请标记出来。谢谢
counter = 0
sentence = 'Hello World'
split = str.split(sentence)

for str in split:
  c = split[counter]
  scramble = c[4] + c[0] + c[3] + c[1] + c[2]
  counter += 1
  print (scramble, end=" ")