diff --git a/binary/bin_to_dec.py b/binary/bin_to_dec.py index 477d7c5..ca8a4a4 100644 --- a/binary/bin_to_dec.py +++ b/binary/bin_to_dec.py @@ -1,7 +1,7 @@ def decimal(binary_str): - ''' Convert binary strings to their decimal equivalents. - Throw ValueError if binary_str contains any characters other than 0 and 1''' + """ Convert binary strings to their decimal equivalents. + Throw ValueError if binary_str contains any characters other than 0 and 1""" remove_0_and_1 = binary_str.replace('0', '').replace('1', '') if len(remove_0_and_1) > 0: diff --git a/camel_case/camel.py b/camel_case/camel.py index a4598d8..42c2f51 100644 --- a/camel_case/camel.py +++ b/camel_case/camel.py @@ -1,14 +1,14 @@ import re def capitalize(word): - ''' Convert word to have uppercase first letter, rest in lowercase''' + """ Convert word to have uppercase first letter, rest in lowercase""" return word[0:1].upper() + word[1:].lower() # Slices don't produce index out of bounds errors. # So this still works on empty strings, strings of length 1 def lowercase(word): - '''convert a word to lowercase''' + """convert a word to lowercase""" return word.lower() diff --git a/cellphones/phone_manager.py b/cellphones/phone_manager.py index 93f4219..0ed843d 100644 --- a/cellphones/phone_manager.py +++ b/cellphones/phone_manager.py @@ -75,14 +75,13 @@ def un_assign(self, phone_id): def phone_info(self, employee): # find phone for employee in phones list - # TODO should return None if the employee does not have a phone - # TODO the method should raise an exception if the employee does not exist + # TODO should return None if the employee does not have a phone + # TODO the method should raise an exception if the employee does not exist for phone in self.phones: if phone.employee_id == employee.id: return phone - return None diff --git a/guest_list/guest_list.py b/guest_list/guest_list.py index f2dfd6e..8497b1e 100644 --- a/guest_list/guest_list.py +++ b/guest_list/guest_list.py @@ -6,13 +6,13 @@ def display_menu(): - print(''' + print(""" 1 Display all entries 2 Add guest 3 Delete guest 4 Search for guest q Quit - ''') + """) print('Please enter your selection: ') return(input()) diff --git a/guest_list/test_guest_list_ui.py b/guest_list/test_guest_list_ui.py index 155e14e..2ae2d1f 100644 --- a/guest_list/test_guest_list_ui.py +++ b/guest_list/test_guest_list_ui.py @@ -6,8 +6,8 @@ class TestGuestListUI(unittest.TestCase): - ''' Test the user interface of the program. Includes various ways of patching the - guest_list.display_menu function''' + """ Test the user interface of the program. Includes various ways of patching the + guest_list.display_menu function""" # Use @patch decorators to patch the entire run of this test function. # These will create a Mock object, and replace the named function with the Mock. diff --git a/miles_db/miles.db b/miles_db/miles.db index 9cf7d15..efffd0d 100644 Binary files a/miles_db/miles.db and b/miles_db/miles.db differ diff --git a/miles_db/miles.py b/miles_db/miles.py index 3d74e68..18b59f4 100644 --- a/miles_db/miles.py +++ b/miles_db/miles.py @@ -4,7 +4,7 @@ """ Before running this code, ensure that miles.db exists and contains the miles table -If not, create expected miles table with +If not, create expected miles table by running the following SQL on the database, create table miles (vehicle text, total_miles float); """ @@ -13,16 +13,22 @@ class MileageError(Exception): def add_miles(vehicle, new_miles): - '''If the vehicle is in the database, increment the number of miles by new_miles + """ If the vehicle is in the database, increment the number of miles by new_miles If the vehicle is not in the database, add the vehicle and set the number of miles to new_miles If the vehicle is None or new_miles is not a positive number, raise MileageError - ''' + """ if not vehicle: raise MileageError('Provide a vehicle name') if not isinstance(new_miles, (int, float)) or new_miles < 0: raise MileageError('Provide a positive number for new miles') + vehicle = vehicle.upper().strip() + + if not vehicle: + raise MileageError('Provide a vehicle name') + + with sqlite3.connect(db_url) as conn: # Attempt to update miles rows_mod = conn.execute('UPDATE MILES SET total_miles = total_miles + ? WHERE vehicle = ?', (new_miles, vehicle)) diff --git a/miles_db/test_miles.db b/miles_db/test_miles.db index f809d0f..0bad6e6 100644 Binary files a/miles_db/test_miles.db and b/miles_db/test_miles.db differ diff --git a/miles_db/test_miles.py b/miles_db/test_miles.py index b7ecede..ba83dc2 100644 --- a/miles_db/test_miles.py +++ b/miles_db/test_miles.py @@ -28,28 +28,49 @@ def setUp(self): def test_add_new_vehicle(self): miles.add_miles('Blue Car', 100) - expected = { 'Blue Car': 100 } + expected = { 'BLUE CAR': 100 } self.compare_db_to_expected(expected) miles.add_miles('Green Car', 50) - expected['Green Car'] = 50 + expected['GREEN CAR'] = 50 self.compare_db_to_expected(expected) def test_increase_miles_for_vehicle(self): miles.add_miles('Red Car', 100) - expected = { 'Red Car': 100 } + expected = { 'RED CAR': 100 } self.compare_db_to_expected(expected) miles.add_miles('Red Car', 50) - expected['Red Car'] = 100 + 50 + expected['RED CAR'] = 100 + 50 self.compare_db_to_expected(expected) + def test_increase_miles_ignores_case(self): + miles.add_miles('Orange Car', 10) + miles.add_miles('ORANGE CAR', 10) + miles.add_miles('oRanGe CaR', 10) + miles.add_miles('orange car', 10) + + expected = { 'ORANGE CAR': 40 } + self.compare_db_to_expected(expected) + + + def test_add_new_vehicle_no_vehicle(self): with self.assertRaises(MileageError): miles.add_miles(None, 100) + + def test_add_new_vehicle_empty_string(self): + with self.assertRaises(MileageError): + miles.add_miles('', 100) + + + def test_add_new_vehicle_blank_string(self): + with self.assertRaises(MileageError): + miles.add_miles(' ', 100) + def test_add_new_vehicle_invalid_new_miles(self): with self.assertRaises(MileageError): diff --git a/mock_api/movie.py b/mock_api/movie.py index c1464b0..00ff3cc 100644 --- a/mock_api/movie.py +++ b/mock_api/movie.py @@ -1,13 +1,13 @@ -''' +""" Install requests with pip install requests -''' +""" import requests import os -''' +""" Before running this code, create an OMDB key set the OMDB_KEY environment variable on your computer You can do it temporarily with the appropriate command, @@ -22,7 +22,7 @@ export OMDB_KEY=abcd1234 Or, you can set permanently. Google will tell you how. -''' +""" movie_base_url = 'http://www.omdbapi.com/' diff --git a/quiz/quiz.py b/quiz/quiz.py new file mode 100644 index 0000000..c864c9d --- /dev/null +++ b/quiz/quiz.py @@ -0,0 +1,33 @@ +def is_correct_answer(user_answer, correct_answer): + """ Check if user's answer is the same as the correct + answer, ignoring case """ + + string_correct_answer = str(correct_answer).lower() + string_user_answer = str(user_answer).lower() + return string_user_answer == string_correct_answer + + + # If you accidentally used the same variable name + # twice in the comparison, for example + + # return string_user_answer == string_user_answer + + # the tests will catch the error, you'll see test fails + # breaking code that works is called a regression + + +def main(): + question = 'How many senators in the US senate?' + answer = 100 + + print(question) + user_answer = input('What is your answer? ') + + if is_correct_answer(user_answer, answer): + print('Correct!') + else: + print(f'Sorry, the answer is {answer}') + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/quiz/test_quiz.py b/quiz/test_quiz.py new file mode 100644 index 0000000..de9d929 --- /dev/null +++ b/quiz/test_quiz.py @@ -0,0 +1,56 @@ +import quiz +import unittest +from unittest import TestCase + +class TestQuiz(TestCase): + + def test_quiz_with_string_answers(self): + # for example, the question might be 'what is the capital of wisconsin' + right_answer = 'Madison' + user_answer = 'Madison' + + result = quiz.is_correct_answer(right_answer, user_answer) + self.assertTrue(result) + + def test_quiz_different_cases(self): + right_answer = 'Madison' # for example, q is capital of wisconsin + user_answer = 'MADISON' # this should be correct + result = quiz.is_correct_answer(right_answer, user_answer) + self.assertTrue(result) + + def test_quiz_reports_wrong_answer(self): + right_answer = 'Madison' + user_answer = 'Saint Paul' + result = quiz.is_correct_answer(right_answer, user_answer) + self.assertFalse(result) + + def test_quiz_numeric_answers_correct_answer(self): + right_answer = 42 + user_answer = 42 + result = quiz.is_correct_answer(right_answer, user_answer) + self.assertTrue(result) + + + def test_quiz_numeric_answer_wrong_answer(self): + right_answer = 100 + user_answer = 1000000 + result = quiz.is_correct_answer(right_answer, user_answer) + self.assertFalse(result) + + + def test_quiz_numeric_and_string_answer_wrong_answer(self): + right_answer = 100 + user_answer = '1000000' + result = quiz.is_correct_answer(right_answer, user_answer) + self.assertFalse(result) + + + def test_quiz_numeric_and_string_answer_right_answer(self): + right_answer = 100 + user_answer = '100' + result = quiz.is_correct_answer(right_answer, user_answer) + self.assertTrue(result) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/recycling_truck/recycling.py b/recycling_truck/recycling.py index 63a6e51..33a9933 100644 --- a/recycling_truck/recycling.py +++ b/recycling_truck/recycling.py @@ -1,4 +1,4 @@ -''' +""" From the Java class :) You are a recycling truck driver. You'd like to collect some statistics on how much each house is recycling. @@ -9,15 +9,15 @@ Figure out the house number with the most recycling, and the number of crates that house recycled. Same for house with the least, and the number of crates for that house. -''' +""" from collections import namedtuple CrateData = namedtuple('CrateData', ['houses', 'crates']) def max_recycling(crates): - '''Returns the index with the largest value in the list and the number of crates for that house. - Raises ValueError if list is empty.''' + """Returns the index with the largest value in the list and the number of crates for that house. + Raises ValueError if list is empty.""" if crates is None or len(crates) == 0: raise ValueError('A list with at least one element is required') @@ -37,9 +37,9 @@ def max_recycling(crates): def min_recycling(crates): - '''Returns the smallest value in the list + """Returns the smallest value in the list and a list of house number (list indexes) with that value. - Raises ValueError if list is None or empty.''' + Raises ValueError if list is None or empty.""" if crates is None or len(crates) == 0: raise ValueError('A list with at least one element is required') @@ -59,7 +59,7 @@ def min_recycling(crates): def total_crates(crates): - ''' Return the total of all the values in the crates list''' + """ Return the total of all the values in the crates list""" total = 0 for crate in crates: @@ -68,7 +68,7 @@ def total_crates(crates): def get_crate_quantities(houses): - ''' Ask user for number of crates for each house''' + """ Ask user for number of crates for each house""" crates = [] for house in range(houses): crates.append(positive_int_input('Enter crates for house {}'.format(house))) @@ -76,7 +76,7 @@ def get_crate_quantities(houses): def positive_int_input(question): - ''' Valdiate user enters a positive integer ''' + """ Valdiate user enters a positive integer """ while True: try: integer = int(input(question + ' ')) diff --git a/recycling_truck/test_recycling_truck.py b/recycling_truck/test_recycling_truck.py index fb999f2..24805ef 100644 --- a/recycling_truck/test_recycling_truck.py +++ b/recycling_truck/test_recycling_truck.py @@ -43,7 +43,7 @@ def test_total(self): def test_get_crate_quantities(self): - ''' + """ Create a patch to replace the built in input function with a mock. The mock is called mock_input, and we can change the way it behaves, e.g. provide our desired return values. So when the code calls input(), instead of @@ -52,7 +52,7 @@ def test_get_crate_quantities(self): list of side_effect values - the first time it is called, it returns the first side_effect value (1), second time it will return the second value, (3) etc... - ''' + """ example_data = [1, 3, 5, 0, 2, 6] with patch('builtins.input', side_effect=example_data) as mock_input: diff --git a/simple_dice/dice.py b/simple_dice/dice.py index 26394da..1706a34 100644 --- a/simple_dice/dice.py +++ b/simple_dice/dice.py @@ -10,7 +10,7 @@ def main(): print('you lose :(') def play(): - ''' roll dice. return True if user wins. ''' + """ roll dice. return True if user wins. """ number_rolled = roll() print('You rolled a %d' % number_rolled) diff --git a/student_lists/studentlists.py b/student_lists/studentlists.py index d52f5da..2a81205 100644 --- a/student_lists/studentlists.py +++ b/student_lists/studentlists.py @@ -22,8 +22,8 @@ def __init__(self, max_students): def add_student(self, student): - ''' Add student if there is space in the class, - Raises Error if student is already in the list ''' + """ Add student if there is space in the class, + Raises Error if student is already in the list """ if len(self.class_list) < self.max_students: if student not in self.class_list: self.class_list.append(student) @@ -32,7 +32,7 @@ def add_student(self, student): def remove_student(self, student): - ''' Remove student from class list. Raises Error if student not in list ''' + """ Remove student from class list. Raises Error if student not in list """ if student not in self.class_list: raise StudentError('Student %s not found in class' % student) @@ -40,13 +40,13 @@ def remove_student(self, student): def is_enrolled(self, student): - ''' Verifies if the student is enrolled or not ''' + """ Verifies if the student is enrolled or not """ return student in self.class_list def index_of_student(self, student): - ''' Returns position of student in list, indexed from 1 - Returns None if student not present ''' + """ Returns position of student in list, indexed from 1 + Returns None if student not present """ if student in self.class_list: return self.class_list.index(student) + 1 return None diff --git a/student_lists/test_studentlists.py b/student_lists/test_studentlists.py index 30cafe0..76636fb 100644 --- a/student_lists/test_studentlists.py +++ b/student_lists/test_studentlists.py @@ -1,4 +1,4 @@ -''' +""" Practice using assertTrue @@ -8,7 +8,7 @@ assertIn assertNotIn -''' +""" from studentlists import ClassList, StudentError from unittest import TestCase diff --git a/triangle/triangle/triangle_math.py b/triangle/triangle/triangle_math.py index a2104d8..44a03e8 100644 --- a/triangle/triangle/triangle_math.py +++ b/triangle/triangle/triangle_math.py @@ -1,10 +1,10 @@ import math def area(base, height): - ''' + """ Compute the area of a triangle with the given base and height. Raises a ValueError if base or height are negative. - ''' + """ if base < 0 or height < 0: raise ValueError('Base and height must be postiive. \ @@ -15,7 +15,7 @@ def area(base, height): def is_right_angle(s1, s2, s3): - ''' Use Pythagoras' Theorum to determine if a triangle is right-angled''' + """ Use Pythagoras' Theorum to determine if a triangle is right-angled""" # A triangle is right-angled if a**2 + b**2 == c**c # where c is the longest side (** is the power operator, a**2 means 'a squared')