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:
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 consultingSignup to hear about my new products and services.My Python posts Subscribe to my blog by emailMy ActiveState recipes
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_functionRun it with:
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()
python joke_bot.pySample 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 consultingSignup to hear about my new products and services.My Python posts Subscribe to my blog by emailMy ActiveState recipes