Chuck Norris Joke Generator in Python

A Python program that uses an API connection to generate random Chuck Norris jokes.

Link to GitHub Repository:

Click Here

The program uses the requests and JSON libraries

import requests
import json

Url variable for api calls

url = "https://api.chucknorris.io/jokes/random"

Function for the api call

def api_call():
    response = requests.request("GET", url) #GET request for new joke
    data = response.text #create a variable for the text of the response
    info = json.loads(data) #load data variable as JSON and assign to new variable
    print('Joke: ', info['value'], '\n') #output of joke to user

Function to open the program

def open_operation():
    print("Welcome to the Chuck Norris Joke Provider") #print Welcome message
    user_input=input("Would you like a joke? Enter Y for new joke\n") #request for user to call for joke or end program
    user_response = user_input.lower() #convert response to lower case for error handling
    if user_response == 'y':
        api_call() #api call if user requests joke
        follow_up() #call the follow-up function for user's next request
    else:
        print("Thank you. The program is closing.") #user does not want a new joke. End program with exit message
        exit()

Function for any follow-up joke requests from the user

def follow_up():
    next_input = input("Would you like another joke? Enter Y for new joke\n") #request for user to call for joke or end program
    next_response = next_input.lower() #convert response to lower case for error handling
    if next_response == 'y':
        api_call() #api call if user requests joke
        follow_up() #call the follow-up function for user's next request
    else:
        print("Thank you. The program is closing.") #user does not want a new joke. End program with exit message
        exit()

Call the open function to begin program

open_operation()

Here is an example of the program running from the user perspective:

image-center