You can download this code by clicking the button below.
This code is now available for download.
This function uses the requests library to fetch a random fact from a public API and returns it.
Technology Stack : requests
Code Type : Python Function
Code Difficulty : Intermediate
import random
import requests
def fetch_random_fact():
# This function fetches a random fact from a public API and returns it.
url = "https://useless-facts.com/random"
response = requests.get(url)
data = response.json()
return data["fact"]
# JSON Explanation