如何利用 Python 生成随机句子?
python 中的随机句子生成
你希望通过四个元组生成随机句子,每个元组包含动词、名词、形容词和副词。以下是解决方法:
import random nouns = ("puppy", "car", "rabbit", "girl", "monkey") verbs = ("runs", "hits", "jumps", "drives", "barfs") adverbs = ("crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally.") adjectives = ("adorable", "clueless", "dirty", "odd", "stupid") # 从每个元组随机选择一个元素 num = random.randrange(0, 5) noun = nouns[num] verb = verbs[num] adverb = adverbs[num] adjective = adjectives[num] # 将元素组合成句子 sentence = noun + ' ' + verb + ' ' + adverb + ' ' + adjective # 打印最终句子 print(sentence)
以上就是如何利用 Python 生成随机句子?的详细内容,更多请关注其它相关文章!