Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions code/zach/lab08-dad-joke-api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import requests

def get_joke(search_parameter):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Great use of function for getting the json data object and creating a python list of jokes.

response = requests.get(f"https://icanhazdadjoke.com/search?term={search_parameter}", headers={
'Accept': 'application/json'
})
data = response.json()["results"]
jokes = []
for item in data:
jokes.append(item['joke'])
return jokes

def main():

user_input = input("Enter a search term to return dad jokes or type 'done' to exit: ")
jokes = get_joke(user_input)
index = 0
answer = 'yes'

print(len(jokes))

if user_input == 'done':
exit
else:
while index < len(jokes) and answer == 'yes':
print(jokes[index])
index += 1

if index == len(jokes):
print("We are out of jokes.")
break

answer = input(f"Would you like to see another dad joke about '{user_input}'? Enter yes/no: ")

print("You're welcome. Byeeeee")

main()