python 中的随机句子生成
你希望通过四个元组生成随机句子,每个元组包含动词、名词、形容词和副词。以下是解决方法:
import randomnouns = ("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 生成随机句子?的详细内容,更多请关注范的资源库其它相关文章!
转载请注明:范的资源库 » 如何利用Python生成随机句子?