1
- import requests
1
+ #Matt Nichols
2
+ #Lab08
3
+
4
+ #Version1
5
+
6
+ import requests
2
7
import pprint
3
- # response = requests.get("https://icanhazdadjoke.com", headers={
4
- # 'Accept': 'application/json'
5
- # })
6
-
7
- #print(response.text)
8
- # joke = response.json()
9
- # print(joke['joke'])
10
-
11
- #version 1
12
- # joke = response.json()
13
- # search_term = input("Input a genre for a joke you'd like to see ")
14
-
15
- #Think about how to do this.. First we need to figure out how to pull a singular joke. secondly
16
-
17
- # response = requests.get(f"https://icanhazdadjoke.com/search?term=${search_term}", headers={
18
- # 'Accept': 'application/json'
19
- # })
20
- # print(response.text)
21
- # joke = response.json()
22
- # first_step = (joke['results'])
23
- # second_step = (first_step[0]['joke'])
24
- # print(second_step)
25
- # for key in range (len(first_step)):
26
- # print(key, first_step[key])
27
-
28
- # # joke = response.json()
29
- # # search_term = joke['golf']
30
- # # print(response.json())
31
- # # joke = response.json()
32
8
33
- response = requests .get ("https://icanhazdadjoke.com" , headers = {
34
- 'Accept' : 'application/json'
35
- })
36
- # print(response)
37
- # print(response.url)
38
- # print(response.encoding)
39
- # print(response.status_code)
40
- # print(response.headers)
41
-
42
- # pprint.pprint(response.text)
43
- # pprint.pprint(response.json())
44
- jokes = response .json ()
45
- pprint .pprint (jokes )
46
- print ("------------------" )
47
- print (jokes )
48
- # print(f'there are {len(jokes)}')
49
- # for joke in jokes:
50
- # print(jokes)
9
+ lines_for_looks = "-----------------------------------------"
10
+ #For future using to seperate the jokes
51
11
12
+ response = requests .get ("https://icanhazdadjoke.com" , headers = {
13
+ 'Accept' : 'application/json' })
14
+
15
+ joke = response .json ()
16
+ #pulling from the dictionary
17
+
18
+ print (f"{ joke ['joke' ]} \n { lines_for_looks } " )
19
+ #print the actual joke itself
20
+
21
+ ###VERSION2
22
+
23
+ #For while loop
24
+ index = 0
25
+
26
+ #Function for allowing the user to view another joke
27
+ def do_keep_going ():
28
+ while True :
29
+ user_input = input ("Would you like to see another joke from this genre? y/n: " )
30
+ if user_input == 'n' :
31
+ return user_input
32
+ if user_input == 'y' :
33
+ return user_input
34
+ print ("Please enter 'y' to continue OR 'n' to exit the program" )
35
+
36
+ #print("Thanks for your time")
37
+ search_term = input ("Enter the genre of joke\n " )
38
+ #For user to input their own genre of joke
39
+
40
+ response = requests .get (f"https://icanhazdadjoke.com/search?term=${ search_term } " , headers = {
41
+ 'Accept' : 'application/json' })
42
+ #New get request with query
43
+
44
+ results = response .json ()['results' ]
45
+ #Pulling from the searchterm and giving a list of jokes
46
+
47
+ number_of_jokes = response .json ()['total_jokes' ]
48
+ #The amount of jokes for the user to see
49
+ print (f"There are { number_of_jokes } jokes in this genre" )
50
+
51
+
52
+ while index < len (results ):
53
+ print (f"{ lines_for_looks } \n { results [index ]['joke' ]} \n { lines_for_looks } " )
54
+ index = index + 1
55
+
56
+ keep_going = do_keep_going ()
57
+ if keep_going == 'n' :
58
+ break
59
+
60
+ number_of_jokes_left = number_of_jokes - index
61
+ print (f"There are { number_of_jokes_left } jokes left from this genre. Thanks for your time." )
62
+
0 commit comments