求助一道python编程题!球球了 救救孩子叭

编写程序自动造句,每个句子包含四个部分:形容词+名词+动词+名词。例如:聪明的阿凡提骑毛驴。(提示:首先创建4个文件保存不同类型的词汇,造句时分别从4个文件中随机选择一个词汇组成句子)

import random #导入随机选择的库

adj = "能干的 主动的 活跃的 灵巧的 机敏的 机灵的 和蔼可亲的 友好的 善于分析的 有理解力的 聪明的"

n = "猫 狗 鸡 兔 人 阿凡提 驴"

v = "骑 坐 打 学" #这一部分可自己输入

#将词库保存到文件中

with open("adj.txt", "w") as adj_file:

adj_file.write(adj)

with open("n.txt", "w") as n_file:

n_file.write(adj)

with open("v.txt", "w") as v_file:

v_file.write(adj)

#将词库从文件中读取出来

with open("adj.txt", "r") as adj_file:

adj = adj_file.read()

with open("n.txt", "r") as n_file:

n = n_file.read()

with open("v.txt", "r") as v_file:

v = v_file.read()

#随机选择词语

adj_list = adj.split(' ')

n_list = n.split(' ')

v_list = v.split(' ')

adj_ = random.choice(adj_list)

n1 = random.choice(n_list)

v_ = random.choice(v_list)

n2 = random.choice(n_list)

#组合词语成句子

result = adj_ + n1 + v_ + n2

print(result)

温馨提示:答案为网友推荐,仅供参考
相似回答