Python编写打油诗

449 篇文章 6 订阅
294 篇文章 6 订阅

Python编写打油诗

利用Python的random库结合创建的不同词性的单词列表,编写一个诗歌生成器程序。

首先创建5个不同词性的单词列表。如下:

(1)名词、动词、形容词、介词、副词列表如下

noun = [
    "fossil",
    "horse",
    "aardvark",
    "judge",
    "chef",
    "mango",
    "extrovert",
    "gorilla",
]
verb = [
    "kicks",
    "jingles",
    "bounces",
    "slurps",
    "meows",
    "explodes",
    "curdles",
]
adjective = [
    "furry",
    "balding",
    "incredulous",
    "fragrant",
    "exuberant",
    "glistening",
]
preposition = [
    "against",
    "after",
    "into",
    "beneath",
    "upon",
    "for",
    "in",
    "like",
    "over",
    "within",
]
adverb = [
    "curiously",
    "extravagantly",
    "tantalizingly",
    "furiously",
    "sensuously",
]

(2)规则:

随机从每类单词中分别选出对应个数的单词:

  • 3个名词
  • 3个动词
  • 3个形容词
  • 2个介词
  • 1个副词

可以借助random模块中的choice()函数,它接收一个列表为参数,随机返回一个列表中的元素。定义一个挑选词汇的函数获取词汇的函数。参数为列表,词的个数。

诗歌的格式如下:

'''
adj代表形容词(adjective),prep代表介词(preposition)
'''
{A/An} {adj1} {noun1}

{A/An} {adj1} {noun1} {verb1} {prep1} the {adj2} {noun2}
{adverb1}, the {noun1} {verb2}
the {noun2} {verb3} {prep2} a/an {adj3} {noun3}

以下是源码:

import random
noun = [
    "fossil",
    "horse",
    "aardvark",
    "judge",
    "chef",
    "mango",
    "extrovert",
    "gorilla",
]
verb = [
    "kicks",
    "jingles",
    "bounces",
    "slurps",
    "meows",
    "explodes",
    "curdles",
]
adjective = [
    "furry",
    "balding",
    "incredulous",
    "fragrant",
    "exuberant",
    "glistening",
]
preposition = [
    "against",
    "after",
    "into",
    "beneath",
    "upon",
    "for",
    "in",
    "like",
    "over",
    "within",
]
adverb = [
    "curiously",
    "extravagantly",
    "tantalizingly",
    "furiously",
    "sensuously",
]

# 定义一个随机挑选词汇的函数
def random_get(wordlst, n):
    if n > 1:
        random_words = []
        while 1:
            word = random.choice(wordlst)

            if word in random_words:
                continue
            random_words.append(word)
            if len(random_words) == n:
                return random_words
    else:
        return random.choice(wordlst) # 单个选词是副词

# 元音字母,用以判断形容词前是an还是a
vowel = ["a", "e", "i", "o", "u"]

# 获取3个形容词
adjective_lst = random_get(adjective, 3)
# 获取3个名词
noun_lst = random_get(noun, 3)
# 获取3个动词
verb_lst = random_get(verb, 3)
# 获取2个介词
preposition_lst = random_get(preposition, 2)
# 获取1个副词
adverb = random_get(adverb, 1)

# 诗歌题目
title = f'{adjective_lst[0]} {noun_lst[0]}'
# 诗歌部分主体
poem = f'{verb_lst[0]} {preposition_lst[0]} the {adjective_lst[1]} {noun_lst[1]}\n{adverb}, the {noun_lst[0]} {verb_lst[1]}\nthe {noun_lst[1]} {verb_lst[2]} {preposition_lst[1]} '

# 判断形容词首字母是不是元音开头,以选择An或A 
if adjective_lst[0][0].lower() in vowel:
    title = 'An ' + title
    if adjective_lst[2][0].lower() in vowel:
        poem = title +'\n\n'+ title + poem + f'an {adjective_lst[2]} {noun_lst[2]}'
    else:

        poem = title +'\n\n'+ title + poem + f'a {adjective_lst[2]} {noun_lst[2]}'
else:
    title = 'A ' + title
    if adjective_lst[2][0].lower() in vowel:
        poem = title + '\n\n' + title + poem + f'an {adjective_lst[2]} {noun_lst[2]}'
    else:
        poem = title + '\n\n' + title + poem + f'a {adjective_lst[2]} {noun_lst[2]}'
print(poem)

这个程序生成的打油诗如下:

A fragrant judge

A fragrant judgemeows beneath the exuberant chef
extravagantly, the judge jingles
the chef bounces after a balding mango
An incredulous extrovert

An incredulous extrovertmeows in the glistening aardvark
extravagantly, the extrovert slurps
the aardvark jingles for a fragrant judge

欢迎关注、点赞、转发!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bruce_Liuxiaowei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值