Skip to content

Commit 4c0c0b4

Browse files
Merge pull request #101 from PdxCodeGuild/jonpan-lab08-dad-joke
jonpan submitting lab08
2 parents 76f7999 + d4ea273 commit 4c0c0b4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
# print(joke)
20+
21+
while True:
22+
x = random.choice(range(len(data)))
23+
joke = data['results'][x]['joke']
24+
print(joke)
25+
more = input("do you want another joke? yes or no. ")
26+
if more == "no":
27+
print("see you later")
28+
break
29+
30+
31+
# for i in range(len(data)):
32+
# print(random.choice(data['results'][i]['joke']))

0 commit comments

Comments
 (0)