-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjokeAPI.py
More file actions
28 lines (23 loc) · 793 Bytes
/
jokeAPI.py
File metadata and controls
28 lines (23 loc) · 793 Bytes
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
import requests
def get_random_joke():
"""Fetch a random joke from the Official Joke API."""
url = "https://official-joke-api.appspot.com/random_joke"
response = requests.get(url)
if response.status_code == 200:
# One line to print the JSON response:
print(f"Full JSON Response: {response.json()}")
joke_data = response.json()
return f"{joke_data['setup']}-{joke_data['punchline']}"
else:
return "Failed to retrieve a joke."
def main():
print("Welcome to the Random Joke Generator!")
while True:
user_input = input("Press Enter to get a new joke or type'q'/'exit' to quit:").strip().lower()
if user_input in ("q", "exit"):
print("Goodbye!")
break
joke = get_random_joke()
print(joke)
if __name__=="__main__":
main()