如何使用Python构建随机故事生成器?
在本节中,我们将制作一个非常有趣的Python初学者级项目。它是一个随机故事生成器。随机故事生成器项目旨在在用户每次执行代码时生成随机故事。一个故事是由一组句子组成的。我们将选择随机短语来构建句子,从而构建故事。
现在,相关的问题是——我们将如何做到这一点?它的答案很简单:
- 我们将首先将故事的元素放在不同的列表中。
- 然后我们将使用随机模块选择不同列表中收集的故事的随机部分。
- 然后将它们连接起来以形成一个故事。
我们将使用 random.choice()函数。在开始之前,让我们看一个关于 random.choice() 如何工作的例子。
Python3
import random
# list of books is stored in the list -'books'
books = ['Mother', 'Midnight Children', 'My experiments with truth']
# An item from the list 'books' is selected
# by random.choice()
print(random.choice(books))
Python3
# Importing random module
import random
Python3
# Defining list of phrases which will help to build a story
Sentence_starter = ['About 100 years ago', ' In the 20 BC', 'Once upon a time']
character = [' there lived a king.',' there was a man named Jack.',
' there lived a farmer.']
time = [' One day', ' One full-moon night']
story_plot = [' he was passing by', ' he was going for a picnic to ']
place = [' the mountains', ' the garden']
second_character = [' he saw a man', ' he saw a young lady']
age = [' who seemed to be in late 20s', ' who seemed very old and feeble']
work = [' searching something.', ' digging a well on roadside.']
Python3
# Selecting an item from each list and concatenating them.
print(random.choice(Sentence_starter)+random.choice(character)+
random.choice(time)+random.choice(story_plot)+
random.choice(place)+random.choice(second_character)+
random.choice(age)+random.choice(work))
Python3
# Importing random module
import random
# Defining list of phrases which will help to build a story
Sentence_starter = ['About 100 years ago', ' In the 20 BC', 'Once upon a time']
character = [' there lived a king.',' there was a man named Jack.',
' there lived a farmer.']
time = [' One day', ' One full-moon night']
story_plot = [' he was passing by',' he was going for a picnic to ']
place = [' the mountains', ' the garden']
second_character = [' he saw a man', ' he saw a young lady']
age = [' who seemed to be in late 20s', ' who seemed very old and feeble']
work = [' searching something.', ' digging a well on roadside.']
# Selecting an item from each list and concatenating them.
print(random.choice(Sentence_starter)+random.choice(character)+
random.choice(time)+random.choice(story_plot) +
random.choice(place)+random.choice(second_character)+
random.choice(age)+random.choice(work))
输出
Midnight Children
正如我们所见,random.choice()函数基本上是从项目列表中选择一个项目。
以下是此随机故事生成器项目中涉及的步骤。
1.导入 random 模块,因为它是Python的内置模块。因此,无需手动安装。
蟒蛇3
# Importing random module
import random
2.定义几个短语列表。在这里,我们定义了八个列表。我们也可以定义更多,这完全取决于我们的选择。
- Sentence_starter – 此列表提供了有关事件时间的概念。
- 字符-此列表讲述这个故事的字符。
- 时间 - 此列表定义了发生某些事件的确切日期。
- story_plot——这个列表定义了故事的情节。
- 地点 - 此列表定义了事件发生的地点。
- second_character – 此列表定义了故事的第二个字符。
- age – 此列表定义了第二个字符的年龄。
- 工作——这个列表讲述了第二个字符正在做的工作。
蟒蛇3
# Defining list of phrases which will help to build a story
Sentence_starter = ['About 100 years ago', ' In the 20 BC', 'Once upon a time']
character = [' there lived a king.',' there was a man named Jack.',
' there lived a farmer.']
time = [' One day', ' One full-moon night']
story_plot = [' he was passing by', ' he was going for a picnic to ']
place = [' the mountains', ' the garden']
second_character = [' he saw a man', ' he saw a young lady']
age = [' who seemed to be in late 20s', ' who seemed very old and feeble']
work = [' searching something.', ' digging a well on roadside.']
3. 在 random.choice() 的帮助下,从每个列表中选择一个项目并连接所选项目以生成故事的句子。
蟒蛇3
# Selecting an item from each list and concatenating them.
print(random.choice(Sentence_starter)+random.choice(character)+
random.choice(time)+random.choice(story_plot)+
random.choice(place)+random.choice(second_character)+
random.choice(age)+random.choice(work))
执行:
让我们在示例的帮助下尝试完整的实现。
蟒蛇3
# Importing random module
import random
# Defining list of phrases which will help to build a story
Sentence_starter = ['About 100 years ago', ' In the 20 BC', 'Once upon a time']
character = [' there lived a king.',' there was a man named Jack.',
' there lived a farmer.']
time = [' One day', ' One full-moon night']
story_plot = [' he was passing by',' he was going for a picnic to ']
place = [' the mountains', ' the garden']
second_character = [' he saw a man', ' he saw a young lady']
age = [' who seemed to be in late 20s', ' who seemed very old and feeble']
work = [' searching something.', ' digging a well on roadside.']
# Selecting an item from each list and concatenating them.
print(random.choice(Sentence_starter)+random.choice(character)+
random.choice(time)+random.choice(story_plot) +
random.choice(place)+random.choice(second_character)+
random.choice(age)+random.choice(work))
输出:
In the 20 BC there lived a king. One day he was going for a picnic to the mountains he saw a man who seemed to be in late 20s digging a well on roadside.
通过这种方式,我们可以根据需要多次编译和运行这段代码。并且会产生不同的短篇小说。