Skip to content

Commit 371f1f0

Browse files
authored
Merge pull request #121 from PdxCodeGuild/daniel-lab08-dad_api
Daniel lab08 dad api
2 parents ab844a1 + 901d7d0 commit 371f1f0

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Lab 08: Dad Joke API
2+
3+
# 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.
4+
# Part 1
5+
6+
# Use the requests library to send an HTTP request to https://icanhazdadjoke.com/ with the Accept header as application/json. This will return a dad joke in JSON format. You can then use the .json() method on the response to get a dictionary. Get the joke out of the dictionary and show it to the user.
7+
8+
# response = requests.get("https://icanhazdadjoke.com", headers={
9+
# 'Accept': 'application/json'
10+
# })
11+
12+
# Part 2
13+
14+
# 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.
15+
16+
# response = requests.get(f"https://icanhazdadjoke.com/search?term=${search_term}", headers={
17+
# 'Accept': 'application/json'
18+
# })
19+
20+
#====================================
21+
22+
# print(response.text) # {"ip":"76.105.187.182"} - raw json response
23+
# data = response.json() # turn raw json into a python dictionary
24+
# print(data) # {'ip': '76.105.187.182'} - python dictionary
25+
# print(data['ip']) # 76.105.187.182
26+
27+
28+
#=======================================================
29+
# Version1
30+
#=======================================================
31+
32+
# import requests
33+
# import time
34+
35+
# response = requests.get("https://icanhazdadjoke.com", headers={'Accept': 'application/json'})
36+
37+
38+
# # print(response.text)
39+
# joke = response.json()
40+
# # print(joke)
41+
42+
# dad_joke = input("Do you wank to hear a dad joke? y or n?: ")
43+
44+
# while True:
45+
# if dad_joke == "y" or dad_joke == "yes":
46+
# print(joke['joke'])
47+
# break
48+
# elif dad_joke == "n" or dad_joke == "no":
49+
# print("goodbye")
50+
# break
51+
# else:
52+
# print("Not a valid answer")
53+
# break
54+
55+
56+
57+
#=======================================================
58+
# Version2
59+
#=======================================================
60+
61+
import requests
62+
import time
63+
64+
65+
search_joke = input("Enter a search term to find a related dad joke: ")
66+
# print(search_joke)
67+
68+
response = requests.get(f"https://icanhazdadjoke.com/search?term=${search_joke}", headers={'Accept': 'application/json'})
69+
# print(response)
70+
71+
72+
73+
joke = response.json()
74+
jokes = joke["results"]
75+
76+
for joke in jokes:
77+
print(joke["joke"])
78+
more_jokes = input("Do you want to hear another dad joke? yes/no: ")
79+
if more_jokes == "yes" or more_jokes == "y":
80+
print(joke["joke"])
81+
elif more_jokes == "no" or more_jokes == "n":
82+
break
83+
else:
84+
print("Not a valid answer")

0 commit comments

Comments
 (0)