Wednesday, July 3, 2024
HomeLanguagesPythonRandfacts library in Python

Randfacts library in Python

In this article, we are going to learn about Randfacts python library that generates random facts. You can use randfacts.get_fact() to return a random fun fact.

Installation:

pip install randfacts

Code for printing the random facts:

Python3




# code
import randfacts
 
 
x = randfacts.get_fact()
print(x)


Output:

People in the United States eat 9 billion chickens and 150 million cattle, 

pigs and sheep annually, and we use around 26 million animals for research, 

95% of which are rodents, birds and fish.

get_fact Function: It is a function that accepts a boolean value. This boolean value determines whether the explicit content filter is on or not. If we pass True to the function, the filter will be on (this is the default) and if we pass False to the function, the filter will be turned off. 

To turn off the filter

Python3




# code
import randfacts
 
 
x = randfacts.get_fact(False)
print(x)


 
 

Output:

Despite assassination plots, an invasion and five decades of economic sanctions, 

Fidel Castro outlasted nine U.S. presidents in power, from Eisenhower to Clinton.

To turn on the filter (default if no argument is given)

Python3




# code
import randfacts
 
 
x = randfacts.get_fact(True)
print(x)


Output:

Anne Frank, Martin Luther King Jr. and Barbara Walters were all born in the same year.

To produce ten interesting facts simultaneously using the loop:

Python3




# code
import randfacts
 
 
for i in range(10):
    x = randfacts.get_fact(True)
    print(x)


Output:

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments