python - 如何通过不同长度的列表生成随机句子?
问题描述
我正在为我的男朋友制作周年纪念礼物,我想向他发送一个程序,让他每次运行时都会有不同的理由让我爱他(他学习计算机科学,这很合适)。
经过研究,我知道我希望它在我说打印后这样做:“我爱 [昵称] 的原因:[我爱他的原因]
将有 6 个昵称和 100 个理由,我希望它每次运行时都返回一句话!
我感谢任何帮助<3
解决方案
我老婆+酒=
import random
nicknames = [
'Panda Bear',
'My Love Love',
'White Chocolate'
]
reasons = [
'the way you look at me',
'your eyes',
'your kisses',
'your cheesy jokes',
'your soft pinkish lips',
'your ear lobes'
]
def get_message(name_list, reason_list):
name = random.choice(name_list)
reason = random.choice(reason_list)
return f'Reason why I love {name}: {reason}.'
# print one random reason
print(get_message(nicknames, reasons))
# print ten random reasons for fun
for n in range(10):
print(get_message(nicknames, reasons))
输出:
Reason why I love Panda Bear: your cheesy jokes.
Reason why I love Panda Bear: your kisses.
Reason why I love My Love Love: your soft pinkish lips.
Reason why I love My Love Love: the way you look at me.
Reason why I love White Chocolate: your cheesy jokes.
Reason why I love White Chocolate: the way you look at me.
Reason why I love My Love Love: the way you look at me.
Reason why I love White Chocolate: your kisses.
Reason why I love White Chocolate: your soft pinkish lips.
Reason why I love My Love Love: your soft pinkish lips.
Reason why I love Panda Bear: your ear lobes.
您可以添加自己的原因和昵称,用逗号分隔并用引号括起来。
推荐阅读
- swift - 如何快速删除 UIMenuController 项目?
- php - Laravel 关系:获取产品的评论和相关用户
- vb.net - RecieveBufferSize OutOfRangeException (.NET)
- cakephp - 在 PHP 3.6 中使用 Cake 时的站点加载问题
- c++ - 常量成员和没有 setter 的私有成员有什么区别?
- python - 我在哪里可以找到python中str方法的实现?
- java - 关闭连接后出现“java.sql.SQLException:连接已关闭”
- android - 为什么我的模拟器显示这个图形故障,之前它工作正常
- python-3.x - 在python中使用OCR从图像中提取特定的文本块
- r - 在 R 中使用 RNN (Keras) 进行时间序列预测