Simple word scrambler

时间:2017-09-07 21:36:28

标签: 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)

The program should rearrange each word in a string into a specific pattern but I cannot figure out how to print the scrambled text onto the same line.

1 个答案:

答案 0 :(得分:1)

你去吧

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=" ")

print函数接受end参数,默认为" \ n"。将其设置为空字符串可防止它在该行的末尾发出新行。