0
我無法弄清楚如何將種子添加到我的程序中。它應該能夠獲得給定的種子值並返回一個混亂的句子。單詞中的第一個和最後一個字母應該保持不變以及結束標點符號。一個單詞中的任何標點符號都可以被混淆。將種子添加到我的程序中(Word字母爭奪)
import random
import string
original_text = input("Enter your text: ").split(' ')
seed = int(input("Enter a seed (0 for random): "))
if seed is not 0:
random.seed(seed)
randomized_list = []
def scramble_word(word):
alpha = word[0]
if word[-1] == "," or "." or "!" or "?" or ":" or ";":
omega = word[-2]
middle = word[1:-2]
else:
omega = word[-1]
middle = word[1:-1]
reorders_text = random.sample(middle, len(middle))
shuffled_text = "".join(reorders_text)
new_words = alpha + shuffled_text + omega
return new_words
for item in original_text:
if len(item) <= 3:
randomized_list.append(item)
else:
randomized_list.append(scramble_word(item))
new_words = " ".join(randomized_list)
print(new_words)
當我隨機化我的中間字符時,是否需要引用種子?對於如何讓單詞的最後一個字母和標點符號保持不變,您也有任何建議嗎? – CluelessCoder
例)你好!應該爭取像Hlelo!或者HOLO!但不是Holle! – CluelessCoder
您可以添加else子句 'else: random.seed() ' – kameranis