Dad Joke Project

Hello devs, this is my first ever blog in software development field. Right now I'm learning Python programming language to achieve web development, automation and data science tasks.

I have taken Colt steele's course in Udemy and I have started to upload the code to my Github account.

I have implemented printing random joke project by using api in Python.

Let's explain the steps I used to achieve the goal.

  • Importing required modules
from pyfiglet import figlet_format
from termcolor import colored
from requests import get
from random import choice
  • Printing Dad Joke Project in figlet format

importing figlet_format method from pyfiglet module which is being intalled by using python3 pip -m install pyfiglet command to style the code to make Dad Joke Project to look like as below by using the code with font style smslant

figlet_format("Dad Joke Project", font="smslant")

Screenshot 2021-09-09 at 12.18.49 PM.png

  • Printing Dad Joke Project in figlet format with green color

importing colored method from termcolor module which is being installed by using python3 pip -m install termcolor command to style the code to make Dad Joke Project to look like as below by using the code with the color green

colored(defualt_text, color="green")

Screenshot 2021-09-09 at 12.23.17 PM.png

  • Sending http request to fetch jokes

importing get method from requests to fetch the list of jokes from the joke api by using the below code for specific joke search term

# specific search term
term = input("Please enter the topic to search joke: \t")
# fetching the api to list jokes for a specific search term
response = get(
    url,
    headers={"Accept": "application/json"},
    params={"term": term}
)
  • Printing the total number of jokes fetched and also printing warning message if total jokes number is equal to 0. After printing the message api fetching message will be called again to fetch a joke as below.
if data['total_jokes'] == 0:
    print(colored("Please enter valid search term", 'red', 'on_cyan', attrs=['concealed']))
    print_joke()

Screenshot 2021-09-09 at 12.50.10 PM.png

  • Printing a joke if the total number of jokes fetched are greater than 0 by using the below code.
elif data['total_jokes'] > 0:
    print(f"You have fetched {data['total_jokes']} jokes and among those below joke is random one\n")
    print(colored(choice(data['results'])['joke'], 'cyan', 'on_grey', attrs=['bold']))

Screenshot 2021-09-09 at 12.55.35 PM.png

I'm attaching my link of the file hosted on github. Any kind of suggestions about my coding skills or writing blog are heartly welcome.

I'm attaching youtube video link to preview the project.

Thank you Colt Steele.