-
Notifications
You must be signed in to change notification settings - Fork 0
/
dad_jokes.py
46 lines (37 loc) · 1.22 KB
/
dad_jokes.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import requests
from random import choice
from termcolor import colored
from pyfiglet import figlet_format
def jokes(term):
url = "https://icanhazdadjoke.com/search"
r = requests.get(
url,
headers={
"Accept": "application/json"},
params={
"term": term,
"limit": 30})
# json requests received in dict data structure (format)
data = r.json()
joke_in_lst = [x["joke"] for x in data["results"]]
total_jokes = data["total_jokes"]
if total_jokes:
joke = choice(joke_in_lst)
if total_jokes > 1:
print(f"\nI've got {total_jokes} jokes. Here's one: \n\n{joke}")
# total_jokes ==1 or total_joke == 0
else:
print(f"\nI've got only 1 joke, here it is ! : \n\n{joke}")
else:
print(f"\nSorry, I don't have jokes about {term} :(")
ascii = figlet_format("Ratio Dad Jokes")
logo = colored(ascii, "cyan")
print(logo)
while True:
jokes(input("Want a dad joke ? Search for a topic: "))
answer = input("Press(q) to quit or hit enter for another dad joke...")
if answer == "q" or answer == "quit":
print(colored("\nSee you next time!", "green"))
break
else:
continue