Python Random Joke
Python Random Joke
ADITYAPUR
SUMMER INTERNSHIP
ON
PYTHON PROGRAMMING
SUBMITTED BY
ELECTRICAL
ENGINEERING
• WEEKLY OVERVIEW
• SOFTWARE REQUIREMENTS
• PROJECT OVERVIEW
• CHALLENGES FACED
• FUTURE OUTLOOK
• CONCLUSION
Types of loops and loop break, continue, pass Mastered loop control
Tuesday
control statements statements statements
Implemented nested
Wednesday Nested loops in Python Nested loop structures
loops
• Operating
System :-
Windows
• Python :-
• Python 3.12(32 bit)
Project
Description :- Joke API
is a API that serves uniformly
and well-formatted jokes. It can
be used without any API token,
membership, registration or
payment. It supports a variety of
filters that can be applied to get
just the right jokes needed.
This Python program is designed to fetch a random joke from the "Official
Joke API" and print it to the console. The program makes use of the requests
library to handle HTTP requests and process the response from the API.
Below is a detailed explanation of each part of the program:
import requests: This line imports the requests library, which is necessary
for making HTTP requests to the API.
url = "https://official-joke-api.appspot.com/random_joke": Specifies the API endpoint for fetching a random joke.
response = requests.get(url): Sends a GET request to the API endpoint and stores the response.
response.raise_for_status(): Raises an HTTPError if the HTTP request returned an unsuccessful status code.
joke = response.json(): Parses the JSON response and stores it in the joke variable.
return f"{joke['setup']} - {joke['punchline']}": Formats the joke as a string and returns it.
The try block handles any exceptions that might occur during the request and parsing process. If an exception occurs, it is
caught and an error message is returned.