Project Idea: Random Jokes Generator
API Used: JokeAPI (https://jokeapi.dev/)
Description: This project will generate random jokes from the JokeAPI and display them in the console. The user can select the type of joke they want (e.g. programming, dark, pun, etc.) and the number of jokes they want to generate.
Steps to create the project:
- Sign up for a free API key from the JokeAPI website.
- Install the requests module in Python using pip. This module will be used to make requests to the API and retrieve the joke data.
- Write a function to make a request to the API and retrieve a random joke. The function should take in parameters for the type of joke and the number of jokes to retrieve.
- Write a main function that prompts the user for the type of joke and the number of jokes they want to generate. The main function should call the random joke function and display the jokes in the console.
- Test the program to make sure it is generating random jokes correctly.
Here’s some sample code to get you started:
import requests
import json
# Function to retrieve random jokes from JokeAPI
def get_random_jokes(joke_type, num_jokes):
url = f”https://v2.jokeapi.dev/joke/{joke_type}?amount={num_jokes}”
response = requests.get(url)
data = json.loads(response.text)
jokes = []
for item in data[‘jokes’]:
if item[‘type’] == ‘single’:
jokes.append(item[‘joke’])
else:
jokes.append(item[‘setup’] + ” ” + item[‘delivery’])
return jokes
# Main function to prompt user for input and generate jokes
def main():
joke_type = input(“What type of joke do you want? (programming, dark, pun, etc.): “)
num_jokes = int(input(“How many jokes do you want to generate?: “))
jokes = get_random_jokes(joke_type, num_jokes)
for joke in jokes:
print(joke)
if __name__ == “__main__”:
main()
This code will generate output as:
To turn this project into a standalone application, you can use a GUI framework such as Tkinter, PyQt, or wxPython. In this example, we’ll use Tkinter to create a simple GUI for the joke generator.
Here are the steps to create an application using Tkinter:
- Install Tkinter (if not already installed) using pip.
- Import the necessary modules for the GUI and the joke generator.
- Create a new class for the GUI and add the necessary widgets (labels, buttons, entry boxes) to the window.
- Write a function to retrieve the input values from the GUI widgets and call the joke generator function.
- Add a label to display the generated jokes in the GUI.
Here’s some sample code to get you started:
import tkinter as tk
import requests
import json
# Function to retrieve random jokes from JokeAPI
def get_random_jokes(joke_type, num_jokes):
url = f”https://v2.jokeapi.dev/joke/{joke_type}?amount={num_jokes}”
response = requests.get(url)
data = json.loads(response.text)
jokes = []
for item in data[‘jokes’]:
if item[‘type’] == ‘single’:
jokes.append(item[‘joke’])
else:
jokes.append(item[‘setup’] + ” ” + item[‘delivery’])
return jokes
# Class for the GUI
class JokeGenerator:
def __init__(self, master):
self.master = master
master.title(“Random Joke Generator”)
# Label for joke type
self.joke_type_label = tk.Label(master, text=”Joke Type:”)
self.joke_type_label.grid(row=0, column=0)
# Entry box for joke type
self.joke_type_entry = tk.Entry(master)
self.joke_type_entry.grid(row=0, column=1)
# Label for number of jokes
self.num_jokes_label = tk.Label(master, text=”Number of Jokes:”)
self.num_jokes_label.grid(row=1, column=0)
# Entry box for number of jokes
self.num_jokes_entry = tk.Entry(master)
self.num_jokes_entry.grid(row=1, column=1)
# Button to generate jokes
self.generate_button = tk.Button(master, text=”Generate Jokes”, command=self.generate_jokes)
self.generate_button.grid(row=2, column=0, columnspan=2)
# Label to display generated jokes
self.jokes_label = tk.Label(master, text=””)
self.jokes_label.grid(row=3, column=0, columnspan=2)
# Function to generate jokes
def generate_jokes(self):
joke_type = self.joke_type_entry.get()
num_jokes = int(self.num_jokes_entry.get())
jokes = get_random_jokes(joke_type, num_jokes)
jokes_str = “\n”.join(jokes)
self.jokes_label.config(text=jokes_str)
# Create the GUI window
root = tk.Tk()
joke_generator = JokeGenerator(root)
root.mainloop()
Output Obtained
If you want to do any project using Python API Contact us at contact@threws.com
#python #programming #coding #java #javascript #programmer #developer #html #snake #coder #code #computerscience #technology #css #machinelearning #pythonprogramming #linux #ballpython #php #datascience #reptile #snakes #reptiles #snakesofinstagram #software #reptilesofinstagram #webdevelopment #webdeveloper #tech #codinglife