我正在尝试在 python 中创建一个程序,该程序从用户那里获取一个句子并混淆所述单词的中间字母,但保持其他字母完整......现在我有代码可以重新排列所有用户输入的内容,只是忘记了空格...我会让我的代码为自己说话..它适用于单个单词输入,我想我会总结一下...我需要随机化用户输入的每个单词,然后保持其他单词完整..
import random
words = input("Enter a word or sentence") #Gets user input
words.split()
for i in list(words.split()): #Runs the code for how many words there are
first_letter = words[0] #Takes the first letter out and defines it
last_letter = words[-1] #Takes the last letter out and defines it
letters = list(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_uncombined = (first_letter, middle_letters, last_letter) #Puts final word all back in place as a list
final_word = "".join(final_word_uncombined) #Puts the list back together again
print(final_word) #Prints out the final word all back together again