diff --git a/code/kaceyb/python/lab08/lab_08.py b/code/kaceyb/python/lab08/lab_08.py index 51ccecbe..9e2e0a4b 100644 --- a/code/kaceyb/python/lab08/lab_08.py +++ b/code/kaceyb/python/lab08/lab_08.py @@ -1,7 +1,5 @@ # PART 1 # - -from logging import exception import requests @@ -41,6 +39,7 @@ joke = response.json() jokes = joke["results"] + try: for joke in jokes: diff --git a/code/kaceyb/python/lab09/lab_09.py b/code/kaceyb/python/lab09/lab_09.py new file mode 100644 index 00000000..b97f75e9 --- /dev/null +++ b/code/kaceyb/python/lab09/lab_09.py @@ -0,0 +1,69 @@ +# 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 # + + + +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 + + +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 + ) + + 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" + + return is_last_page, json_response + + +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 + + 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 + + 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/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/class_lesson_12.py b/code/kaceyb/python/notes/lesson06/class_lesson_12.py new file mode 100644 index 00000000..0dd08822 --- /dev/null +++ b/code/kaceyb/python/notes/lesson06/class_lesson_12.py @@ -0,0 +1,38 @@ +# 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) diff --git a/code/kaceyb/python/notes/lesson06/lesson_09.py b/code/kaceyb/python/notes/lesson06/lesson_09.py index d50ef902..7077f418 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,6 +12,36 @@ # 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) + + +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.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 + +p1 = (5, 2) +p2 = Point(8, 4) + def distance(p1, p2): dx = p1['x'] - p2['x'] dy = p1['y'] - p2['y'] @@ -41,16 +71,13 @@ def distance(self, p): 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/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 diff --git a/code/kaceyb/python/notes/lesson06/mini_capstone.py b/code/kaceyb/python/notes/lesson06/mini_capstone.py new file mode 100644 index 00000000..e69de29b diff --git a/code/kaceyb/python/notes/lesson06/notes.py b/code/kaceyb/python/notes/lesson06/notes.py index 76713805..d7536eb4 100644 --- a/code/kaceyb/python/notes/lesson06/notes.py +++ b/code/kaceyb/python/notes/lesson06/notes.py @@ -1,5 +1,7 @@ # with open('example.txt', 'a') as file: # text = file.write('\nMORE TEXT') + +# print(text) # print(text) # file = open('example2.txt', 'w') @@ -9,6 +11,10 @@ # with open('colors.txt') as file: # colors = file.read() + +# colors = file.read().split() +# print(colors) + # colors = file.read().split() # print(colors) @@ -17,6 +23,9 @@ # text = file.write() # print(text) +# import datetime + +# read file # import datetime #read file @@ -34,6 +43,11 @@ # with open('phonebook.txt') as file: # phone_book = file.read() + +# phone_book = phone_book.split('\n') +# name = input('Lookup name: ') + + # phone_book = phone_book.split('\n') # name = input('Lookup name: ') @@ -43,11 +57,20 @@ # 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)) + # phone_book.append(name + " " + phone_number) @@ -55,7 +78,6 @@ # with open('phonebook2.txt', 'w') as file2: # file2.write('\n'.join(phone_book)) - # print(type(phone_book)) # print(phone_book) # def Convert(lst): @@ -66,4 +88,6 @@ # color = file.read() # color = color.split('\n') # color = Convert(color) -# print(color) \ No newline at end of file +# print(color) + +# print(color) diff --git a/code/kaceyb/python/notes/lesson06/notes2.py b/code/kaceyb/python/notes/lesson06/notes2.py index d7e7d229..0349f855 100644 --- a/code/kaceyb/python/notes/lesson06/notes2.py +++ b/code/kaceyb/python/notes/lesson06/notes2.py @@ -25,6 +25,10 @@ # # for category in categories: # # print(category) + +# for i in range(len(categories)): +# print(i, categories[i]) + # for i in range(len(categories)): # print(i, categories[i]) @@ -42,16 +46,23 @@ 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) -