Python中的 Randfacts 库
在本文中,我们将学习生成随机事实的Randfacts Python库。您可以使用 randfacts.get_fact() 返回随机有趣的事实。
安装:
pip install randfacts
打印随机事实的代码:
Python3
# code
import randfacts
x = randfacts.get_fact()
print(x)
Python3
# code
import randfacts
x = randfacts.get_fact(False)
print(x)
Python3
# code
import randfacts
x = randfacts.get_fact(True)
print(x)
Python3
# code
import randfacts
for i in range(10):
x = randfacts.get_fact(True)
print(x)
输出:
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函数:它是一个接受布尔值的函数。此布尔值确定是否打开显式内容过滤器。如果我们将 True 传递给函数,过滤器将打开(这是默认值),如果我们将 False 传递给函数,过滤器将关闭。
关闭过滤器
蟒蛇3
# code
import randfacts
x = randfacts.get_fact(False)
print(x)
输出:
Despite assassination plots, an invasion and five decades of economic sanctions,
Fidel Castro outlasted nine U.S. presidents in power, from Eisenhower to Clinton.
打开过滤器(如果没有给出参数,则为默认值)
蟒蛇3
# code
import randfacts
x = randfacts.get_fact(True)
print(x)
输出:
Anne Frank, Martin Luther King Jr. and Barbara Walters were all born in the same year.
使用循环同时产生十个有趣的事实:
蟒蛇3
# code
import randfacts
for i in range(10):
x = randfacts.get_fact(True)
print(x)
输出: