From 0109b707bccd2dd4fe8ebfdaffde559c6079ec51 Mon Sep 17 00:00:00 2001 From: Kacey Broetje Date: Tue, 26 Jul 2022 12:35:04 -0700 Subject: [PATCH 1/3] initial commit --- code/kaceyb/python/lab09/lab_09.py | 68 +++++++++++++++++++ .../python/notes/lesson06/class_lesson_12.py | 40 +++++++++++ .../kaceyb/python/notes/lesson06/contacts.csv | 0 .../kaceyb/python/notes/lesson06/lesson_11.py | 0 .../kaceyb/python/notes/lesson06/location.csv | 1 + 5 files changed, 109 insertions(+) create mode 100644 code/kaceyb/python/lab09/lab_09.py create mode 100644 code/kaceyb/python/notes/lesson06/class_lesson_12.py create mode 100644 code/kaceyb/python/notes/lesson06/contacts.csv create mode 100644 code/kaceyb/python/notes/lesson06/lesson_11.py create mode 100644 code/kaceyb/python/notes/lesson06/location.csv diff --git a/code/kaceyb/python/lab09/lab_09.py b/code/kaceyb/python/lab09/lab_09.py new file mode 100644 index 00000000..467febd1 --- /dev/null +++ b/code/kaceyb/python/lab09/lab_09.py @@ -0,0 +1,68 @@ +# VERSION 1 # + + +import json +import requests + +# response = requests.get("https://favqs.com/api/qotd") + +# +# print(response) +# print(type(response)) +# print(response.text) +# print(response.status_code) + +# quote = response.text +# json_response = response.json() + +# quote_author = json_response['quote']['author'] + +# quote_body = json_response['quote']['body'] + +# print(f'The author is {quote_author}\nTheir quote: {quote_body}') + + + + +# VERSION 2 # + + + +import pprint + + + + + +page_number = 1 + +search_term = 'everything' + + +response = requests.get(f"https://favqs.com/api/quotes?page={page_number}&filter={search_term}", + headers = {'Authorization': 'Token token="855df50978dc9afd6bf86579913c9f8b"'},) + + +json_response = response.json() +# pprint.pprint(json_response) +is_last_page = json_response['last_page'] + + +# is_last_page == +# pprint.pprint(json_response['quotes']) +# pprint.pprint(json_response['quotes'][0]) + +quotes_list = [] + +for i in range(len(json_response['quotes'])): + + author_json = json_response['quotes'][i]['author'] + quote_json = json_response['quotes'][i]['body'] + + quote_dictionary = { + 'author': author_json, + 'quote': quote_json + } + quotes_list.append(quote_dictionary) +pprint.pprint(quotes_list) +# print(len(json_response['quotes'])) diff --git a/code/kaceyb/python/notes/lesson06/class_lesson_12.py b/code/kaceyb/python/notes/lesson06/class_lesson_12.py new file mode 100644 index 00000000..b0735603 --- /dev/null +++ b/code/kaceyb/python/notes/lesson06/class_lesson_12.py @@ -0,0 +1,40 @@ +# with open('location.csv', 'w') as file: +# file.write("Hello World") + +# # with open(file, 'r') as f: +# # contents = f.readlines() +# # print(contents) + +# with open(file, 'r') as f: +# for line in f: +# list.append(line) + +# phonebook = {'Dave': '42342', 'Alice': '234234'} +# with open(file, 'w') as location: +# for name, number in location.items(): +# line = f'{name}{number}\n' +# location.write(line) + + + +#CSV = comma separated values + +# with open('contacts.csv', 'w') as file: +# lines = file.read().split('\n') +# print(lines) + +contacts = [ + {'name':'matthew', 'favorite fruit':'blackberries', 'favorite color':'orange'}, + {'name':'sam', 'favorite fruit':'pineapple', 'favorite color':'purple'}, + {'name': 'teeto', 'favorite fruit':'lychee', 'favorite color':'brown'} +] + +f = open('contacts.csv') + +with open('contacts.csv', 'r') as contacts: + contacts_data = contacts.read().split('\n') + +dict = [] +for line in contacts_data: + dict.append(contacts_data) + \ No newline at end of file diff --git a/code/kaceyb/python/notes/lesson06/contacts.csv b/code/kaceyb/python/notes/lesson06/contacts.csv new file mode 100644 index 00000000..e69de29b diff --git a/code/kaceyb/python/notes/lesson06/lesson_11.py b/code/kaceyb/python/notes/lesson06/lesson_11.py new file mode 100644 index 00000000..e69de29b diff --git a/code/kaceyb/python/notes/lesson06/location.csv b/code/kaceyb/python/notes/lesson06/location.csv new file mode 100644 index 00000000..5e1c309d --- /dev/null +++ b/code/kaceyb/python/notes/lesson06/location.csv @@ -0,0 +1 @@ +Hello World \ No newline at end of file From 9d98f08d71198799e21669bb28a5c420bcda758c Mon Sep 17 00:00:00 2001 From: Kacey Broetje Date: Tue, 26 Jul 2022 13:48:55 -0700 Subject: [PATCH 2/3] finished quotes lab --- code/kaceyb/python/lab09/lab_09.py | 59 ++++++++++--------- .../python/notes/lesson06/class_lesson_12.py | 26 ++++---- .../kaceyb/python/notes/lesson06/lesson_09.py | 40 ++++++------- code/kaceyb/python/notes/lesson06/notes.py | 24 ++++---- code/kaceyb/python/notes/lesson06/notes2.py | 17 +++--- 5 files changed, 81 insertions(+), 85 deletions(-) diff --git a/code/kaceyb/python/lab09/lab_09.py b/code/kaceyb/python/lab09/lab_09.py index 467febd1..b97f75e9 100644 --- a/code/kaceyb/python/lab09/lab_09.py +++ b/code/kaceyb/python/lab09/lab_09.py @@ -22,47 +22,48 @@ # print(f'The author is {quote_author}\nTheir quote: {quote_body}') - - # VERSION 2 # -import pprint - - - +page_number = 1 # Starts page_number at page 1 +search_term = input("Enter a keyword to search for quotes: ") # Gives the user an input to search for term -page_number = 1 -search_term = 'everything' +def get_response(search_term): # Make a function to make code reusable and keep program clean + """ Pulls JSON from API and converts it into a dictionary + Args: + search_term (string): Term that the API uses to search with + + Returns: + boolean: True if last page, False if not + dictionary: API response(big jumbled mess of dictionary key/value pairs) + """ + response = requests.get( + f"https://favqs.com/api/quotes?page={page_number}&filter={search_term}", # F string that builds the API URL + headers={"Authorization": 'Token token="855df50978dc9afd6bf86579913c9f8b"'}, # API Token which gives access [200]=Success [401]=Unathorized + ) -response = requests.get(f"https://favqs.com/api/quotes?page={page_number}&filter={search_term}", - headers = {'Authorization': 'Token token="855df50978dc9afd6bf86579913c9f8b"'},) + json_response = response.json() # creates dictionary from JSON + 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" -json_response = response.json() -# pprint.pprint(json_response) -is_last_page = json_response['last_page'] + return is_last_page, json_response -# is_last_page == -# pprint.pprint(json_response['quotes']) -# pprint.pprint(json_response['quotes'][0]) +is_last_page = False # Assigning False so the loop will run at least once +quotes_list = [] # Assigning an Empty list to the quotes_list(initiating dictionary) +while is_last_page == False: # Beginning while loop + is_last_page, json_response = get_response(search_term) # Calling function and assigning responses + for i in range(len(json_response["quotes"])): # For loop to loop through all the quotes -quotes_list = [] + author_json = json_response["quotes"][i]["author"] # Get author from quotes for the current quote, i + quote_json = json_response["quotes"][i]["body"] # Get quote body from quotes for the current quote, i -for i in range(len(json_response['quotes'])): - - author_json = json_response['quotes'][i]['author'] - quote_json = json_response['quotes'][i]['body'] - - quote_dictionary = { - 'author': author_json, - 'quote': quote_json - } - quotes_list.append(quote_dictionary) -pprint.pprint(quotes_list) -# print(len(json_response['quotes'])) + print(f'"{quote_json}"\nBy {author_json}\n') # Print formatted string to display the quote and author + user_is_done = input("enter 'next page' or 'done': ").lower().strip() # Ask the user if they want to continue + page_number += 1 # Incrementing page_number by 1 each loop + if user_is_done == "done": # Taking user inputs "done" and breaking the loop ending the program + break diff --git a/code/kaceyb/python/notes/lesson06/class_lesson_12.py b/code/kaceyb/python/notes/lesson06/class_lesson_12.py index b0735603..0dd08822 100644 --- a/code/kaceyb/python/notes/lesson06/class_lesson_12.py +++ b/code/kaceyb/python/notes/lesson06/class_lesson_12.py @@ -1,6 +1,6 @@ # with open('location.csv', 'w') as file: # file.write("Hello World") - + # # with open(file, 'r') as f: # # contents = f.readlines() # # print(contents) @@ -8,7 +8,7 @@ # with open(file, 'r') as f: # for line in f: # list.append(line) - + # phonebook = {'Dave': '42342', 'Alice': '234234'} # with open(file, 'w') as location: # for name, number in location.items(): @@ -16,25 +16,23 @@ # location.write(line) - -#CSV = comma separated values +# CSV = comma separated values # with open('contacts.csv', 'w') as file: # lines = file.read().split('\n') # print(lines) - + contacts = [ - {'name':'matthew', 'favorite fruit':'blackberries', 'favorite color':'orange'}, - {'name':'sam', 'favorite fruit':'pineapple', 'favorite color':'purple'}, - {'name': 'teeto', 'favorite fruit':'lychee', 'favorite color':'brown'} + {"name": "matthew", "favorite fruit": "blackberries", "favorite color": "orange"}, + {"name": "sam", "favorite fruit": "pineapple", "favorite color": "purple"}, + {"name": "teeto", "favorite fruit": "lychee", "favorite color": "brown"}, ] - -f = open('contacts.csv') -with open('contacts.csv', 'r') as contacts: - contacts_data = contacts.read().split('\n') - +f = open("contacts.csv") + +with open("contacts.csv", "r") as contacts: + contacts_data = contacts.read().split("\n") + dict = [] for line in contacts_data: dict.append(contacts_data) - \ No newline at end of file diff --git a/code/kaceyb/python/notes/lesson06/lesson_09.py b/code/kaceyb/python/notes/lesson06/lesson_09.py index d50ef902..872d6e92 100644 --- a/code/kaceyb/python/notes/lesson06/lesson_09.py +++ b/code/kaceyb/python/notes/lesson06/lesson_09.py @@ -4,7 +4,7 @@ # def __init__(self, username, email): # self.username = username # self.email = email - + # bruce = User('criticbruce', 'cd@email.com') # print(bruce.username) @@ -12,45 +12,43 @@ # SECOND PIECE # import math + + def distance(p1, p2): - dx = p1['x'] - p2['x'] - dy = p1['y'] - p2['y'] - return math.sqrt(dx*dx + dy*dy) + dx = p1["x"] - p2["x"] + dy = p1["y"] - p2["y"] + return math.sqrt(dx * dx + dy * dy) + -p1 = {'x': 5, 'y':2} -p2 = {'x': 8, 'y':4} +p1 = {"x": 5, "y": 2} +p2 = {"x": 8, "y": 4} print(distance(p1, p2)) + class Point: def __init__(self, x, y): - self.x = x + self.x = x self.y = y - + def distance(self, p): dx = self.x - p.x dy = self.y - p.y - return math.sqrt(dx*dx + dy*dy) - -p1 = Point(5, 2) # call the initializer, instantiate the class + return math.sqrt(dx * dx + dy * dy) + + +p1 = Point(5, 2) # call the initializer, instantiate the class -p1 = (5,2) -p2 = Point(8,4) +p1 = (5, 2) +p2 = Point(8, 4) # print(p1.x) # print(p1.y) print(type(p1)) # print(p1.distance(p2)) -name = [1,2,3] +name = [1, 2, 3] print(type(name)) print(name.sort()) number = input("What's your favorite number?: ") print(type(number)) - - - - - - - diff --git a/code/kaceyb/python/notes/lesson06/notes.py b/code/kaceyb/python/notes/lesson06/notes.py index 76713805..6c408ce3 100644 --- a/code/kaceyb/python/notes/lesson06/notes.py +++ b/code/kaceyb/python/notes/lesson06/notes.py @@ -1,6 +1,6 @@ # with open('example.txt', 'a') as file: # text = file.write('\nMORE TEXT') -# print(text) +# print(text) # file = open('example2.txt', 'w') # text = file.write('Legacy Method') @@ -9,17 +9,17 @@ # with open('colors.txt') as file: # colors = file.read() - + # colors = file.read().split() # print(colors) - + # with open('colors.txt', 'w') as file: # text = file.write() # print(text) -# import datetime +# import datetime -#read file +# read file # start = datetime.datetime.now() # with open('phonebook.txt') as file: @@ -34,27 +34,27 @@ # with open('phonebook.txt') as file: # phone_book = file.read() - + # phone_book = phone_book.split('\n') # name = input('Lookup name: ') - + # found_entry = False # for entry in phone_book: # if name.lower() in entry.lower(): # print(entry) # found_entry = True - + # if not found_entry: # print('Contact not found') # name = input('Enter new contact name: ') # phone_number = input(f'Enter the number for : {name}') - + # phone_book.append(name + " " + phone_number) - + # phone_book.sort() # with open('phonebook2.txt', 'w') as file2: # file2.write('\n'.join(phone_book)) - + # print(type(phone_book)) # print(phone_book) @@ -66,4 +66,4 @@ # color = file.read() # color = color.split('\n') # color = Convert(color) -# print(color) \ No newline at end of file +# print(color) diff --git a/code/kaceyb/python/notes/lesson06/notes2.py b/code/kaceyb/python/notes/lesson06/notes2.py index d7e7d229..a2de9342 100644 --- a/code/kaceyb/python/notes/lesson06/notes2.py +++ b/code/kaceyb/python/notes/lesson06/notes2.py @@ -25,10 +25,10 @@ # # for category in categories: # # print(category) - + # for i in range(len(categories)): # print(i, categories[i]) - + # choice = int(input('Select a category: ')) # query = { @@ -42,16 +42,15 @@ import requests -url = 'https://ghibliapi.herokuapp.com/films' + +url = "https://ghibliapi.herokuapp.com/films" response = requests.get(url) # print(response.text) data = response.json() # print(data[0]['title']) for film in data: - print(film['title']) - print(film['release_date']) - print(film['description']) - - - print('-'*10) + print(film["title"]) + print(film["release_date"]) + print(film["description"]) + print("-" * 10) From 7cfe01e40d8316873e0f98d42b51fd801269b375 Mon Sep 17 00:00:00 2001 From: Kacey Broetje Date: Wed, 27 Jul 2022 18:03:10 -0700 Subject: [PATCH 3/3] submitting lab10 ATM --- code/kaceyb/python/lab10/lab_10.py | 74 +++++++++++++++++++ code/kaceyb/python/notes/lesson06/colors.txt | 1 - code/kaceyb/python/notes/lesson06/colors4.txt | 4 - code/kaceyb/python/notes/lesson06/example.txt | 2 - .../kaceyb/python/notes/lesson06/example2.txt | 1 - .../{contacts.csv => mini_capstone.py} | 0 6 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 code/kaceyb/python/lab10/lab_10.py delete mode 100644 code/kaceyb/python/notes/lesson06/colors.txt delete mode 100644 code/kaceyb/python/notes/lesson06/colors4.txt delete mode 100644 code/kaceyb/python/notes/lesson06/example.txt delete mode 100644 code/kaceyb/python/notes/lesson06/example2.txt rename code/kaceyb/python/notes/lesson06/{contacts.csv => mini_capstone.py} (100%) diff --git a/code/kaceyb/python/lab10/lab_10.py b/code/kaceyb/python/lab10/lab_10.py new file mode 100644 index 00000000..6a7a55f7 --- /dev/null +++ b/code/kaceyb/python/lab10/lab_10.py @@ -0,0 +1,74 @@ +class ATM: + def __init__(self): + self.balance = 0 + self.transactions = [] + + def check_balance(self): + return round(self.balance, 2) + + def deposit(self, amount): + self.balance = self.balance + amount + self.transactions.append(f'user deposited ${amount}') + return self.balance + + def withdraw(self, amount): + self.balance = self.balance - amount + self.transactions.append(f'user withdrew ${amount}') + return self.balance + + def check_withdrawal(self, amount): + if amount > self.balance: + return False + return True + + def calc_interest(self): + calc_interest = (self.balance * .1) / 100 + return round(calc_interest, 2) + + def print_transactions(self): + for transaction in self.transactions: + print(transaction) + + + + + + + +atm = ATM() # create an instance of our class +print('Welcome to the ATM') +while True: + command = input('Enter a command: ') + if command == 'balance': + balance = atm.check_balance() # call the check_balance() method + print(f'Your balance is ${balance}') + elif command == 'deposit': + amount = float(input('How much would you like to deposit? ')) + atm.deposit(amount) # call the deposit(amount) method + print(f'Deposited ${amount}') + elif command == 'withdraw': + amount = float(input('How much would you like ')) + if atm.check_withdrawal(amount): # call the check_withdrawal(amount) method + atm.withdraw(amount) # call the withdraw(amount) method + print(f'Withdrew ${amount}') + else: + print('Insufficient funds') + elif command == 'interest': + amount = atm.calc_interest() # call the calc_interest() method + atm.deposit(amount) + print(f'Accumulated ${amount} in interest') + elif command == 'transactions': + atm.print_transactions() + elif command == 'help': + print('Available commands:') + print('balance - get the current balance') + print('deposit - deposit money') + print('withdraw - withdraw money') + print('interest - accumulate interest') + print('transactions - list of all transactions') + print('exit - exit the program') + + elif command == 'exit': + break + else: + print('Command not recognized') \ No newline at end of file diff --git a/code/kaceyb/python/notes/lesson06/colors.txt b/code/kaceyb/python/notes/lesson06/colors.txt deleted file mode 100644 index 782d3d30..00000000 --- a/code/kaceyb/python/notes/lesson06/colors.txt +++ /dev/null @@ -1 +0,0 @@ -red, green, yellow, blue, indigo, chartuce \ No newline at end of file diff --git a/code/kaceyb/python/notes/lesson06/colors4.txt b/code/kaceyb/python/notes/lesson06/colors4.txt deleted file mode 100644 index b300e03b..00000000 --- a/code/kaceyb/python/notes/lesson06/colors4.txt +++ /dev/null @@ -1,4 +0,0 @@ -green -yellow -indigo -chartuce diff --git a/code/kaceyb/python/notes/lesson06/example.txt b/code/kaceyb/python/notes/lesson06/example.txt deleted file mode 100644 index e12cf818..00000000 --- a/code/kaceyb/python/notes/lesson06/example.txt +++ /dev/null @@ -1,2 +0,0 @@ -Adding content to file -MORE TEXT \ No newline at end of file diff --git a/code/kaceyb/python/notes/lesson06/example2.txt b/code/kaceyb/python/notes/lesson06/example2.txt deleted file mode 100644 index c4d093d8..00000000 --- a/code/kaceyb/python/notes/lesson06/example2.txt +++ /dev/null @@ -1 +0,0 @@ -Legacy Method \ No newline at end of file diff --git a/code/kaceyb/python/notes/lesson06/contacts.csv b/code/kaceyb/python/notes/lesson06/mini_capstone.py similarity index 100% rename from code/kaceyb/python/notes/lesson06/contacts.csv rename to code/kaceyb/python/notes/lesson06/mini_capstone.py