Random Fact Fetcher Function

  • Share this:

Code introduction


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                
              
Tags: