Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to make a fun fact generator

+17 votes
asked Apr 20 by victor c (220 points)

1 Answer

+3 votes
answered Apr 26 by naazih (210 points)

You need three things:

  1. A collection of facts
  2. A way to randomly select one
  3. A way to display it (console, web page, app, etc.)

1. python code:

import random

facts = [
    "Octopuses have three hearts.",
    "Bananas are berries, but strawberries aren't.",
    "Honey never spoils.",
    "A day on Venus is longer than a year on Venus.",
    "Wombats have cube-shaped poop."
]

def get_fun_fact():
    return random.choice(facts)

print(" Fun Fact:")
print(get_fun_fact())

2. java script:

<!DOCTYPE html>
<html>
<head>
    <title>Fun Fact Generator</title>
</head>
<body>

<h1>Fun Fact Generator </h1>
<button onclick="showFact()">Get a Fun Fact</button>
<p id="fact"></p>

<script>
const facts = [
    "Octopuses have three hearts.",
    "Bananas are berries.",
    "Honey never spoils.",
    "Sharks existed before trees.",
    "A group of flamingos is called a flamboyance."
];

function showFact() {
    const randomIndex = Math.floor(Math.random() * facts.length);
    document.getElementById("fact").textContent = facts[randomIndex];
}
</script>

</body>
</html>

you can make it more interesting, instead of stopping at random facts, you can level it up

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...