我正在尝试创建一个打乱单词和句子的程序。我有打乱单词的代码,但我不知道该怎么做才能打乱一个句子。有任何想法吗?提前致谢!!
import random
def main():
word = input("Please enter a word or a sentence (if sentence, add a period to end the sentence: ")
if "." in word:
print(scramble(word))
else:
print(scrambleTwo(word))
def scramble(word):
char1 = random.randint(1, len(word)-2)
char2 = random.randint(1, len(word)-2)
while char1 == char2:
char2 = random.randint(1, len(word)-2)
newWord = ""
for i in range(len(word)):
if i == char1:
newWord = newWord + word[char2]
elif i == char2:
newWord = newWord + word[char1]
else:
newWord = newWord + word[i]
return newWord
def scrambleTwo(word):
main()