【发布时间】:2012-10-24 15:42:32
【问题描述】:
我正在尝试在 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
【问题讨论】:
标签: python list random split shuffle