Akansha Yadav builds a Python story generator that randomly picks a character, action, and place, then combines them into sentences like "Once upon a time, a brave knight discovered treasure in a mystical castle." Uses the random library and lists to create different stories each time you run it.
Chapters
Let's create a story generator using Python programming language. Here I will be using the basic concepts of Python and the library. So here I'm importing a library called random, then creating a function generate story. Then I will be using 3 things over here. This is going to be a text adventure using story, so I'm going to create 3 variables: character, action, and place. So whatever character you want, you can add it up. Then actions also you can add it up over here, like it should be related to like horror or whatever, you can add it. Then places also, like dark forest, mystical castle, enchanted cave. And then I'm randomly generating all 3 things using random.choice method of random library. Then I'm generating a story headline: once upon a time, a character, action, in a place. This is how the sequence will go on. And then I'm printing the story and calling out the function. So let's test the file over here. So let me run the file. After running the file, we must get the output with the headline. So let's run it. Yeah, here we got, once upon a time, a brave inside discovered a hidden treasure in the mystical castle. Let's run it again to just test it whether it is randomly generating or not. Once upon a time, a present dragon discovered a hidden treasure in the enchanted cave. So yes, it is.