You need three things:
- A collection of facts
- A way to randomly select one
- 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