Home » A Python program that displays a random quote

A Python program that displays a random quote

by pqzrmred71

Here’s a Python program that displays a random quote each time it’s run.

This program can be enhanced to display quotes in a loop or even fetch random quotes from an online API.

However, here we will keep it simple by storing a list of quotes in the code and selecting one randomly.

Code

import random

# List of quotes
quotes = [
    "The only limit to our realization of tomorrow is our doubts of today. – Franklin D. Roosevelt",
    "Do not wait to strike till the iron is hot; but make it hot by striking. – William Butler Yeats",
    "Whether you think you can or think you can’t, you’re right. – Henry Ford",
    "The future belongs to those who believe in the beauty of their dreams. – Eleanor Roosevelt",
    "It does not matter how slowly you go as long as you do not stop. – Confucius",
    "Act as if what you do makes a difference. It does. – William James",
    "Success is not how high you have climbed, but how you make a positive difference to the world. – Roy T. Bennett",
    "Life is 10% what happens to us and 90% how we react to it. – Charles R. Swindoll",
    "The best way to predict the future is to create it. – Peter Drucker",
    "You miss 100% of the shots you don’t take. – Wayne Gretzky"
]

# Function to get a random quote
def get_random_quote():
    return random.choice(quotes)

# Main program to display a random quote
def main():
    print("Random Quote Generator")
    print("----------------------")
    print(get_random_quote())

# Run the main program
if __name__ == "__main__":
    main()

Explanation

  1. Import the Required Library:
    • random: This library provides the choice() function, which selects a random item from a list.
  2. List of Quotes:
    • quotes: A list containing several quotes. Each quote is a string, and they are stored within square brackets ([]) separated by commas. You can add as many quotes as you like.
  3. Define get_random_quote Function:
    • This function returns a random quote from the quotes list using random.choice().
    • return random.choice(quotes): The choice() function selects one item randomly from the quotes list each time it’s called.
  4. Main Program to Display a Random Quote:
    • Header:
      • print(“Random Quote Generator”) and print(“———————-“) print a title and a separator line for better readability.
    • Display the Quote:
      • print(get_random_quote()): Calls get_random_quote() to get a random quote and prints it to the console.
  5. Run the Program:
    • The if __name__ == “__main__”: block ensures that main() runs only when this file is executed directly, not when it’s imported as a module.

How the Program Works

  1. When you run the program, it prints a title and a separator line.
  2. It calls get_random_quote() to select a random quote from the list.
  3. The selected quote is displayed on the console.

Sample Output

Each time you run the program, you’ll see a different quote (if there are enough quotes).

Example:

>>> %Run get_random_quote.py
Random Quote Generator
----------------------
The best way to predict the future is to create it. – Peter Drucker
>>> %Run get_random_quote.py
Random Quote Generator
----------------------
Whether you think you can or think you can’t, you’re right. – Henry Ford
>>> %Run get_random_quote.py
Random Quote Generator
----------------------
Life is 10% what happens to us and 90% how we react to it. – Charles R. Swindoll

Notes

  • Random Selection: random.choice(quotes) ensures a different quote appears each time (though there could be repeats since it’s random).
  • Extending the List: You can easily add more quotes to the quotes list by adding additional strings to the list.
  • Using APIs: For dynamically fetching quotes, you can replace quotes with an API call to a quotes API, such as https://api.quotable.io/random.

Summary

This Python program is a simple random quote generator that displays a different quote each time it runs.

It uses the random.choice() function to select a random quote from a predefined list, making it easy to expand or modify.

This is a great program to practice list handling and random selection in Python.

You may also like