Random Quote Fetcher

  • Share this:

Code introduction


This function fetches a random quote from https://api.quotable.io/random. If the request is successful, it returns the quote content; otherwise, it returns an error message.


Technology Stack : Requests

Code Type : Function

Code Difficulty : Intermediate


                
                    
import requests
import random

def get_random_quote():
    url = 'https://api.quotable.io/random'
    response = requests.get(url)
    if response.status_code == 200:
        return response.json()['content']
    else:
        return "Sorry, I couldn't fetch a quote at the moment."                
              
Tags: