-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdad_joke.py
30 lines (23 loc) · 899 Bytes
/
dad_joke.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def dad_joke():
import requests, random, termcolor, pyfiglet
termcolor.cprint(pyfiglet.figlet_format("DAD JOKE BOT"),
"red" )
url = "https://icanhazdadjoke.com/search"
term = input("Select a topic (or type q to quit): ")
while term != "q":
data = requests.get(
url,
headers={"Accept": "application/json"},
params={"term": term}
).json()
s = data["total_jokes"]
if s > 1:
print(f"I have {s} jokes about that! Here's one:\n" +
random.choice(data["results"])["joke"])
elif not s:
print("I have don't have any jokes about that! Try again:")
else:
print("I have one joke about that! Here it is:\n" +
data["results"][0]["joke"])
term = input("Select a topic (or type q to quit): ")
dad_joke()