Python Madlibs and story generators for kids

Part ACrazy text Madlibs in Python with User Input

To start with, we are going to use this website to run Python 3:

We can create variables in Python from user input. Print is used to show text. In Python 3, we put f before a string if it has a variable inside.

Try running the example.

name = input('What is your name? ')
print(f'Hello {name}.')

Activity 1:

Now try asking their favorite color. Create a new variable color, and call for input asking for their favorite color.
Print a response from the computer, such as I like _____ too!

Activity 2:

This is a piece of code to create a poem. Find the bugs and fix them. The color of the words can help you find the mistakes.
We use \n for line breaks. Add the missing line breaks to make it into a 4 line poem.

Poem1 = input('Type a color: ')
poem2 = input('Type a plural noun: ')
poem3 = input('Type a plural noun: ')
poem4 = input(Type an adjective: )

print(f'Roses are {word1},\n {word2} are blue,{word3} are {word4} and so are you.')

Activity 3:

Here is a madlib story. Can you find and fix the mistakes?
word1 = input('Type a noun: ') word2 = input('Type a plural noun: ') word3 = input('Type a place: ') Word4 =input('Type a time of day: ') word5 = input('Type your name: ') word6 == input('Type a number: ') word7 = input('Type a another number: ') word8 = input('Type a plural noun: ') word9 = input 'Type a verb: ' print("\n) print('Giant {Word1} Attacks {word3}') print(f'By: {word5.') print f'The people of {word3} are confused and afraid after a giant {word1} attacked {word3} yesterday {word4}. {word6} people died and {word7} were badly hurt in the attack. People tried throwing {word2} and fighter jets shot {word8} at the giant {word1}, but it was of no use. Police are advising anyone who sees the giant {word1} to {word9}.'

Part B

A Python Story Generator with lists

Let's go one step further and make a story generator that doesn't require user input. We will create lists of words and then pick them randomly.
First let's create a list and print its contents.
words = ["I", "cat", "you", "am", "dinosaur", "alien", "?", "a"]

We can print list items like so:
Print (words[4])

Activity 1:

Paste both lines of code into Python. Which word does it print? Why that word?

Activity 2:

What does the following code print:
print (f'{words[0]} {words[3]} {words[7]} {words[4]}{words[9]}')

Activity 3:

Change the numbers in the code and try to make your own sentence.

Activity 4:

We can also use randomly generated numbers to choose random words. First we need to tell python to get ready to use random numbers.
Start your code with:
from random import randint

And then try running this code:
words = ["me", "cat", "you", "dinosaur", "alien", "fish", "robot"]
print (f'You are a {words[randint(0,6)]}.')

Run the code several times and see if you get the same answer.

Activity 5

Add four more words to each list and change the range of the random numbers:

from random import randint
nouns = ["cat", "princess", ]
adjectives = ["green", "ugly", ]
places = ["the beach", "Suntek", ]

adj1 = adjectives[randint(0,1)]
noun1 = nouns[randint(0,1)]
noun2 = nouns[randint(0,1)]
place1 = places[randint(0,1)]

print (f' Once upon a time there was a {adj1} {noun1} . The {noun1} lived in a {noun2} in the middle of {place1}.')

Activity 6

Now try to create your own game!

from random import randint #wild pokemon wpok = ["Pikachu", "Charmander", "Bulbasaur", "Squirtle", "Mew", "Mewtwo" ] game = "on"
def pokfight(): fightmon = wpok[randint(0,5)] outcome = [ "You caught it!", f"The wild {fightmon} escaped", "The pokemon defeats you. You go to the Pokecenter to heal and then continue your search for Pokemon.", ]
print(f'You have encountered a wild {fightmon}.') q1 = input('Do you want to fight or run? ') if q1 == 'fight': print(outcome[randint(0,2)]) print('Welcome to Pokemon Safari. Try to catch as many pokemon as you can!') print('You walk for a bit and then finally see something.') while game != 'over': pokfight() print('You continue to search for Pokemon.') print(f'The Pokemon kills you.')