1
+ #Matt Nichols
2
+ #Lab08
3
+
4
+ #Version1
5
+
6
+ import requests
7
+ import pprint
8
+
9
+ lines_for_looks = "-----------------------------------------"
10
+ #For future using to seperate the jokes
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