Open In App

Find jokes on a user provided topics using Python

Last Updated : 24 Jan, 2021
Like Article

Prerequisites :

Python requests module helps us make Http requests to specified URL. In this article we will make a json request to this url: and develop a project wherein the user enters any word and the output would be a joke related to that word. 

We will be using API (library request) to search jokes using a keyword entered by the user if we get multiple results on the keyword entered then one of those jokes will be displayed randomly.

Modules Used

  • requests module allows us to sent the Https requests using python
  • pyfiglet module converts the ASCII texts to ASCII art fonts
  • termcolor module helps us format color in the output terminal
  • random module generates random numbers numbers in Python


  • Import module
  • Add header, to specify in for, is data to be stored, by default it is html text(Here, we are getting data in JSON format)
  • Ask user for input
  • Pass input as searching element to retrieve data from URL
  • Print retrieved data.



import requests
import pyfiglet
import termcolor
from random import choice
header = pyfiglet.figlet_format("Find a joke!")
header = termcolor.colored(header, color="white")
term = input("Let me tell you a joke! Give me a topic: ")
response_json = requests.get("",
                             headers={"Accept": "application/json"}, params={"term": term}).json()
results = response_json["results"]
total_jokes = response_json["total_jokes"]
if total_jokes > 1:
    print(f"I've got {total_jokes} jokes about {term}. Here's one:\n", choice(
elif total_jokes == 1:
    print(f"I've got one joke about {term}. Here it is:\n", results[0]['joke'])
    print(f"Sorry, I don't have any jokes about {term}! Please try again.")


The keyword passes was banana, one of the joke related to is displayed

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads