Skip to content

Commit 9d98f08

Browse files
committed
finished quotes lab
1 parent 0109b70 commit 9d98f08

File tree

5 files changed

+81
-85
lines changed

5 files changed

+81
-85
lines changed

code/kaceyb/python/lab09/lab_09.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,48 @@
2222
# print(f'The author is {quote_author}\nTheir quote: {quote_body}')
2323

2424

25-
26-
2725
# VERSION 2 #
2826

2927

3028

31-
import pprint
32-
33-
34-
29+
page_number = 1 # Starts page_number at page 1
3530

31+
search_term = input("Enter a keyword to search for quotes: ") # Gives the user an input to search for term
3632

37-
page_number = 1
3833

39-
search_term = 'everything'
34+
def get_response(search_term): # Make a function to make code reusable and keep program clean
35+
""" Pulls JSON from API and converts it into a dictionary
4036
37+
Args:
38+
search_term (string): Term that the API uses to search with
39+
40+
Returns:
41+
boolean: True if last page, False if not
42+
dictionary: API response(big jumbled mess of dictionary key/value pairs)
43+
"""
44+
response = requests.get(
45+
f"https://favqs.com/api/quotes?page={page_number}&filter={search_term}", # F string that builds the API URL
46+
headers={"Authorization": 'Token token="855df50978dc9afd6bf86579913c9f8b"'}, # API Token which gives access [200]=Success [401]=Unathorized
47+
)
4148

42-
response = requests.get(f"https://favqs.com/api/quotes?page={page_number}&filter={search_term}",
43-
headers = {'Authorization': 'Token token="855df50978dc9afd6bf86579913c9f8b"'},)
49+
json_response = response.json() # creates dictionary from JSON
4450

51+
is_last_page = json_response["last_page"] # "last_page is a field inside JSON_response which returns True or False THEN assigns it to is_last_page"
4552

46-
json_response = response.json()
47-
# pprint.pprint(json_response)
48-
is_last_page = json_response['last_page']
53+
return is_last_page, json_response
4954

5055

51-
# is_last_page ==
52-
# pprint.pprint(json_response['quotes'])
53-
# pprint.pprint(json_response['quotes'][0])
56+
is_last_page = False # Assigning False so the loop will run at least once
57+
quotes_list = [] # Assigning an Empty list to the quotes_list(initiating dictionary)
58+
while is_last_page == False: # Beginning while loop
59+
is_last_page, json_response = get_response(search_term) # Calling function and assigning responses
60+
for i in range(len(json_response["quotes"])): # For loop to loop through all the quotes
5461

55-
quotes_list = []
62+
author_json = json_response["quotes"][i]["author"] # Get author from quotes for the current quote, i
63+
quote_json = json_response["quotes"][i]["body"] # Get quote body from quotes for the current quote, i
5664

57-
for i in range(len(json_response['quotes'])):
58-
59-
author_json = json_response['quotes'][i]['author']
60-
quote_json = json_response['quotes'][i]['body']
61-
62-
quote_dictionary = {
63-
'author': author_json,
64-
'quote': quote_json
65-
}
66-
quotes_list.append(quote_dictionary)
67-
pprint.pprint(quotes_list)
68-
# print(len(json_response['quotes']))
65+
print(f'"{quote_json}"\nBy {author_json}\n') # Print formatted string to display the quote and author
66+
user_is_done = input("enter 'next page' or 'done': ").lower().strip() # Ask the user if they want to continue
67+
page_number += 1 # Incrementing page_number by 1 each loop
68+
if user_is_done == "done": # Taking user inputs "done" and breaking the loop ending the program
69+
break
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
# with open('location.csv', 'w') as file:
22
# file.write("Hello World")
3-
3+
44
# # with open(file, 'r') as f:
55
# # contents = f.readlines()
66
# # print(contents)
77

88
# with open(file, 'r') as f:
99
# for line in f:
1010
# list.append(line)
11-
11+
1212
# phonebook = {'Dave': '42342', 'Alice': '234234'}
1313
# with open(file, 'w') as location:
1414
# for name, number in location.items():
1515
# line = f'{name}{number}\n'
1616
# location.write(line)
1717

1818

19-
20-
#CSV = comma separated values
19+
# CSV = comma separated values
2120

2221
# with open('contacts.csv', 'w') as file:
2322
# lines = file.read().split('\n')
2423
# print(lines)
25-
24+
2625
contacts = [
27-
{'name':'matthew', 'favorite fruit':'blackberries', 'favorite color':'orange'},
28-
{'name':'sam', 'favorite fruit':'pineapple', 'favorite color':'purple'},
29-
{'name': 'teeto', 'favorite fruit':'lychee', 'favorite color':'brown'}
26+
{"name": "matthew", "favorite fruit": "blackberries", "favorite color": "orange"},
27+
{"name": "sam", "favorite fruit": "pineapple", "favorite color": "purple"},
28+
{"name": "teeto", "favorite fruit": "lychee", "favorite color": "brown"},
3029
]
31-
32-
f = open('contacts.csv')
3330

34-
with open('contacts.csv', 'r') as contacts:
35-
contacts_data = contacts.read().split('\n')
36-
31+
f = open("contacts.csv")
32+
33+
with open("contacts.csv", "r") as contacts:
34+
contacts_data = contacts.read().split("\n")
35+
3736
dict = []
3837
for line in contacts_data:
3938
dict.append(contacts_data)
40-

code/kaceyb/python/notes/lesson06/lesson_09.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,51 @@
44
# def __init__(self, username, email):
55
# self.username = username
66
# self.email = email
7-
7+
88
# bruce = User('criticbruce', '[email protected]')
99

1010
# print(bruce.username)
1111

1212
# SECOND PIECE #
1313

1414
import math
15+
16+
1517
def distance(p1, p2):
16-
dx = p1['x'] - p2['x']
17-
dy = p1['y'] - p2['y']
18-
return math.sqrt(dx*dx + dy*dy)
18+
dx = p1["x"] - p2["x"]
19+
dy = p1["y"] - p2["y"]
20+
return math.sqrt(dx * dx + dy * dy)
21+
1922

20-
p1 = {'x': 5, 'y':2}
21-
p2 = {'x': 8, 'y':4}
23+
p1 = {"x": 5, "y": 2}
24+
p2 = {"x": 8, "y": 4}
2225
print(distance(p1, p2))
2326

27+
2428
class Point:
2529
def __init__(self, x, y):
26-
self.x = x
30+
self.x = x
2731
self.y = y
28-
32+
2933
def distance(self, p):
3034
dx = self.x - p.x
3135
dy = self.y - p.y
32-
return math.sqrt(dx*dx + dy*dy)
33-
34-
p1 = Point(5, 2) # call the initializer, instantiate the class
36+
return math.sqrt(dx * dx + dy * dy)
37+
38+
39+
p1 = Point(5, 2) # call the initializer, instantiate the class
3540

36-
p1 = (5,2)
37-
p2 = Point(8,4)
41+
p1 = (5, 2)
42+
p2 = Point(8, 4)
3843

3944
# print(p1.x)
4045
# print(p1.y)
4146
print(type(p1))
4247
# print(p1.distance(p2))
4348

44-
name = [1,2,3]
49+
name = [1, 2, 3]
4550
print(type(name))
4651
print(name.sort())
4752

4853
number = input("What's your favorite number?: ")
4954
print(type(number))
50-
51-
52-
53-
54-
55-
56-
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# with open('example.txt', 'a') as file:
22
# text = file.write('\nMORE TEXT')
3-
# print(text)
3+
# print(text)
44

55
# file = open('example2.txt', 'w')
66
# text = file.write('Legacy Method')
@@ -9,17 +9,17 @@
99

1010
# with open('colors.txt') as file:
1111
# colors = file.read()
12-
12+
1313
# colors = file.read().split()
1414
# print(colors)
15-
15+
1616
# with open('colors.txt', 'w') as file:
1717
# text = file.write()
1818
# print(text)
1919

20-
# import datetime
20+
# import datetime
2121

22-
#read file
22+
# read file
2323

2424
# start = datetime.datetime.now()
2525
# with open('phonebook.txt') as file:
@@ -34,27 +34,27 @@
3434

3535
# with open('phonebook.txt') as file:
3636
# phone_book = file.read()
37-
37+
3838
# phone_book = phone_book.split('\n')
3939
# name = input('Lookup name: ')
40-
40+
4141
# found_entry = False
4242
# for entry in phone_book:
4343
# if name.lower() in entry.lower():
4444
# print(entry)
4545
# found_entry = True
46-
46+
4747
# if not found_entry:
4848
# print('Contact not found')
4949
# name = input('Enter new contact name: ')
5050
# phone_number = input(f'Enter the number for : {name}')
51-
51+
5252
# phone_book.append(name + " " + phone_number)
53-
53+
5454
# phone_book.sort()
5555
# with open('phonebook2.txt', 'w') as file2:
5656
# file2.write('\n'.join(phone_book))
57-
57+
5858

5959
# print(type(phone_book))
6060
# print(phone_book)
@@ -66,4 +66,4 @@
6666
# color = file.read()
6767
# color = color.split('\n')
6868
# color = Convert(color)
69-
# print(color)
69+
# print(color)

code/kaceyb/python/notes/lesson06/notes2.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626
# # for category in categories:
2727
# # print(category)
28-
28+
2929
# for i in range(len(categories)):
3030
# print(i, categories[i])
31-
31+
3232
# choice = int(input('Select a category: '))
3333

3434
# query = {
@@ -42,16 +42,15 @@
4242

4343

4444
import requests
45-
url = 'https://ghibliapi.herokuapp.com/films'
45+
46+
url = "https://ghibliapi.herokuapp.com/films"
4647
response = requests.get(url)
4748
# print(response.text)
4849
data = response.json()
4950
# print(data[0]['title'])
5051
for film in data:
51-
print(film['title'])
52-
print(film['release_date'])
53-
print(film['description'])
54-
55-
56-
print('-'*10)
52+
print(film["title"])
53+
print(film["release_date"])
54+
print(film["description"])
5755

56+
print("-" * 10)

0 commit comments

Comments
 (0)