可能的重复:
Python 拆分字符串并混淆中间
我有一个 python 程序,我真的被困住了,我这样做的目的是从用户那里取一个句子,然后把单词的中间弄乱,让外面的字母保持完整......我的老师说我需要做点什么参数,但我真的不知道如何访问或使用我的功能......
import random
user_words = input("Enter a word or sentence: ") #Gets user input
word = list(user_words.split()) #Creating a word list of the user input
def Jumble_One_Word(x):
if len(user_words) > 2: #Only need to change words longer than 2
first_letter = user_words[0] #Takes the first letter out and defines it
last_letter = user_words[-1] #Takes the last letter out and defines it
letters = list(user_words[1:-1]) #Takes the rest and puts them into a list
random.shuffle(letters) #shuffles the list above
middle_letters = "".join(letters) #Joins the shuffled list
final_word = "".join([first_letter, middle_letters, last_letter]) #Puts final word all back in place as a list
print(final_word)#Prints out the final word all back together again
Jumbler_Count = -1
for i in range(len(word)):
Jumbler_Count + 1
Word1 = Jumble_One_Word(word[Jumbler_Count])
示例输入:HELLO MY NAME IS OUTPUT:HLELO MY NMAE IS RESULTED 输出:H leai El moymns Hoi ynmeEa lm ls Hiyn l oelEamms H naellmEyo m is