Nonsensical Knock Knock in Python

And so it was that – partly out of curiosity and partly out of sheer geekery – I purchased a Raspberry Pi B+ over the Christmas break.

It will be for the kids (honest!) but they are just too that bit too young for it at the moment. However, it can still beĀ pressed into service as a joke machine – as long as the jokes are as basic and silly as my Python skills.

So, here isĀ “Knock Knock” as our three year old thinks it goes:

#!/usr/bin/env python

# import the random and time modules
import random, time

# set up the arrays
first_name = ['Fish','Beetle','Cat','Balloon','Wobbly','Eyeball','Slug','Dragon']
last_name = ['Bottom','Face','Body','Head','Legs','Ears','Eyeball','Man','Lady']

# prompt the user
raw_input("Knock knock.")
raw_input("Who's there?")

# randomly select an item from the first name array
first_name_choice = random.choice(first_name)

# think for half a second
time.sleep(0.5)

# prompt the user with the first name
print "%s" % first_name_choice
raw_input("%s who?" % first_name_choice)

# randomly select an item from the second name array
last_name_choice = random.choice(last_name)

# think for half a second
time.sleep(0.5)

# print both
print "%s %s" % (first_name_choice, last_name_choice)

It will print the following:

Knock knock.
Who's there?
Slug.
Slug who?
Slug Face.

Cue the ROFL. Save the file as knockknock.py and then call it from the command line with:

python knockknock.py

And there you have it. A slice of comedy gold on the Raspberry Pi.