Skip to content

Commit e091dee

Browse files
committed
submitting lab08
1 parent b4e7fa2 commit e091dee

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

code/jonpan/lab08_v1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#Lab08 Use the Dad Joke API to get a dad joke and display it to the user. You may want to also use time.sleep to add suspense.
2+
3+
import requests
4+
import time
5+
6+
response = requests.get("https://icanhazdadjoke.com", headers={'Accept': 'application/json'})
7+
data = response.json()
8+
9+
print(data['joke'])

code/jonpan/lab08_v2.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#Lab08 V2: Add the ability to "search" for jokes using another endpoint. Create a REPL that allows one to enter a search term and go through jokes one at a time.
2+
3+
import requests
4+
import time
5+
import pprint
6+
import random
7+
8+
search_term = input("Enter a word to search jokes for: ")
9+
10+
response = requests.get(f"https://icanhazdadjoke.com/search?term=${search_term}", headers={
11+
'Accept': 'application/json'
12+
})
13+
data = response.json()
14+
15+
# pprint.pprint(data)
16+
17+
x = random.choice(range(len(data)))
18+
joke = data['results'][x]['joke']
19+
20+
print(joke)
21+
22+
23+
# for i in range(len(data)):
24+
# print(random.choice(data['results'][i]['joke']))

0 commit comments

Comments
 (0)