Sunday, April 17, 2016

A simple joke bot in Python

By Vasudev Ram


Image attribution: Vasudev Ram

Following a chain of thoughts, including about bots and jokes, I had the idea of writing a simple joke bot in Python. Here it is, in file joke_bot.py:
from __future__ import print_function
import sys
import os
from random import randint

'''
A joke bot in Python.
v1.0.
Author: Vasudev Ram
Copyright 2016 Vasudev Ram - http://jugad2.blogspot.com 
'''

jokes = [
    '''
    Q: Why did the chicken cross the road?
    A: To get to the other side.
    ''',
    '''
    Q: What is black, white and red all over?
    A: A newspaper.
    ''',
    '''
    Q: What time is it when an elephant sits on your fence?
    A: Time to build a new fence.
    ''',
    '''
    Q: How many elephants will fit into a Mini?
    A: Four: Two in the front, two in the back.
    Q: How many giraffes will fit into a Mini?
    A: None. It's full of elephants.
    ''',
    '''
    Q: What do elephants have that nothing else has?
    A: Baby elephants.
    ''',
    '''
    Knock Knock.
    Who's there?
    Apple.
    Apple Who?
    Apple-y ever after.
    ''',
    '''
    Knock Knock.
    Who's there?
    Amos.
    Amos Who?
    A mosquite bit me.
    Knock Knock.
    Who's there?
    Andy.
    Andy Who?
    Andy's still biting me!
    ''',
    '''
    Knock Knock.
    Who's there?
    Orange.
    Orange Who?
    Orange you going to the party?
    ''',
]

lj = len(jokes)

def tell_a_joke():
    i = randint(0, lj - 1)
    print("Here is a joke for you:")
    print(jokes[i])

def clear_screen():
    # For Windows.
    os.system('cls')
    # For Unix/Linux.
    #os.system('clear')
    # Add clear screen support for 
    # other OS's here if needed.

def main():
    clear_screen()
    print('\nPython Joke Bot v1.0 activated.\n')
    ans = ''
    while ans != 'n':
        tell_a_joke()
        ans = raw_input('Tell another one? [YyNn]: ')
        ans = ans.strip().lower()
        clear_screen()
    print('See you next Funday.')

main()
Run it with:
python joke_bot.py
Sample output:
Python Joke Bot v1.0 activated.

Here is a joke for you:

    Knock Knock.
    Who's there?
    Apple.
    Apple Who?
    Apple-y ever after.

Tell another one? [YyNn]: y

Here is a joke for you:

    Q: How many elephants will fit into a Mini?
    A: Four: Two in the front, two in the back.
    Q: How many giraffes will fit into a Mini?
    A: None. It's full of elephants.

Tell another one? [YyNn]:

Here is a joke for you:

    Knock Knock.
    Who's there?
    Amos.
    Amos Who?
    A mosquite bit me.
    Knock Knock.
    Who's there?
    Andy.
    Andy Who?
    Andy's still biting me!
Here is a joke for you:

    Knock Knock.
    Who's there?
    Amos.
    Amos Who?
    A mosquite bit me.
    Knock Knock.
    Who's there?
    Andy.
    Andy Who?
    Andy's still biting me!

Tell another one? [YyNn]:

Here is a joke for you:

    Q: What time is it when an elephant sits on your fence?
    A: Time to build a new fence.

Tell another one? [YyNn]:

Here is a joke for you:

    Knock Knock.
    Who's there?
    Apple.
    Apple Who?
    Apple-y ever after.

Tell another one? [YyNn]: n

See you next Funday.

A point about the program: it's obviously quite simple. I was almost not going to post about it because of that, but then realized that this, as well as some other small programs I've written in the past and plan to write in the future, though small now, can still illustrate a few points about programming (at least for beginners, including me, a perpetual beginner :).

And, more importantly, it can be built upon incrementally over time, in multiple versions, to illustrate various other programming language and library features. E.g. I can modify/enhance this program to read the jokes from a flat or structured file (such as JSON or XML), a key-value store like BSD DB (supported by the Python stdlib), SQLite (ditto), etc.

See you next Funday :)

- Vasudev Ram - Online Python training and consulting

Signup to hear about my new products and services.

My Python posts     Subscribe to my blog by email

My ActiveState recipes


3 comments:

Vasudev Ram said...

Realized (too late for this version) that it would have been more realistic if, instead of:

"Here is a joke for you:"

I had used something like:

"Stop me if you've heard this before",

or other such variants :)

And more than one, shown by rotation, randomized ...

Anonymous said...

It's also not much foolproof: you could answer anything else than 'N' or 'n' to get a new joke (not only 'Y' or 'y').

Vasudev Ram said...

Thanks for your comment.

First of all, it was not necessarily meant to be foolproof.

There are too many fools in the world for that :)

Put another way, a quick Google search for "jokes about foolproof" shows quotes like:

"It is impossible to make anything foolproof because fools are so ingenious."

Just kidding.

Anyway:

That can be fixed by changing this line:

ans = raw_input('Tell another one? [YyNn]: ')

to this:

ans = raw_input('Enter any other key for more, N or n to quit: ')

The other way is to check for and accept Y/y/N/n only, of course, and
put the raw_input() call in a loop until that is true.

Maybe it was a bug, or maybe I left it in on purpose, to check whether people are actually reading the code ... I'm not telling :)

I had a school chemistry teacher who used to do that sometimes - state wrong chemical facts sometimes to see if we were sleeping. I often noticed them :)