Kids Coding
Project: Story Generator
Pick random words from lists and print a new story every run.
KidsIntermediateProject: Story Generator
A new story every run
Keep lists of names, places, and verbs. random.choice picks one from each. Join them into sentences.
Story generator
import random
names = ["Sam", "Riley", "Jun"]
places = ["the moon", "a bus", "the library"]
verbs = ["danced", "sneezed", "sang"]
who = random.choice(names)
where = random.choice(places)
did = random.choice(verbs)
print(who + " " + did + " on " + where + ".")
print("Nobody expected that.")Swap the word lists and the same story machine tells a brand-new tale. That reuse is the real prize, not the story itself.
Grow it
Add a second sentence. Add a list of objects. Print a title.
Real life
A campfire story where someone picks random cards: who, where, what they did.
Shuffle three piles. Draw one from each. That is random.choice.
Campfire cards
import random
who = random.choice(["a fox", "a robot", "a pirate"])
where = random.choice(["the bus", "the moon", "the kitchen"])
print("Once", who, "got stuck on", where + ".")Change one word. Run it. That is how this idea shows up outside the lesson.
Try it
Exercise 1: Pick a random animal from cat, owl, or frog and print Today's animal is plus that animal.
Show solution
import random
animal = random.choice(["cat", "owl", "frog"])
print("Today's animal is " + animal)