Jul-23-2020, 01:33 PM
PLEASE NOTE: This is a school assignment so dear fellow Pythonistas and forum members: Please provide as many tips and hints as possible without providing a solution until I am able to come up with a complete solution on my own.
Here is my assignment:
My teacher has provided some further instructions:
Here is the unit test provided:
So this is the basic function that I am starting with:
What I am struggling with is the following two assertion checks, specifically handling exceptions. For example when
The first doc I found on Google related to zero division in Python was from a chapter titled Python “Programming / Exceptions” under the sub-section titled: “Catching exceptions”. It’s slightly outdated. Even though it’s written for Python 2, I found it to be easy to follow. To adapt the guide for Python 3, I referred to the official Python doc on porting Python 2 code to Python 3, specifically the section on “Capturing the Currently Raised Exception”. I also used Sebastina Raschka’s “The key differences between Python 2.7.x and Python 3.x with examples” on “Handling exceptions”. I also found a detailed guide on ZeroDivisionErrors titled “Python Exception Handling – ZeroDivisionError”. However it was verbose and hard to follow. I found Dan Bader + Said van de Klundert's guide on Python Exceptions to be enormously helpful as well.
What might you people recommend I explore next? Again, I am looking for tips and hints. Please do not complete the assignment for me.
Here is my assignment:
Quote:https://codechalleng.es/bites/110/
In this Bite you complete the divide_numbers function that takes a numerator and a denominator (the number above and below the line respectively when doing a division).
First you try to convert them to ints, if that raises a ValueError you will re-raise it (using raise).
To keep things simple we can expect this function to be called with int/float/str types only (read the tests why ...)
Getting passed that exception (no early bail out, we're still in business) you try to divide numerator by denominator returning its result.
If denominator is 0 though, Python throws another exception. Figure out which one that is and catch it. In that case return 0.
My teacher has provided some further instructions:
Quote:For this exercise you can assume numerator and denominator are of type int/str/float. Try to convert numerator and denominator to int types, if that raises a ValueError, reraise it. Following, do the division and return the result. However if denominator is 0 catch the corresponding exception Python throws (cannot divide by 0), and return 0
Here is the unit test provided:
import pytest from division3 import divide_numbers @pytest.mark.parametrize("numerator, denominator, expected", [ (1, 2, 0.5), (8, 2, 4), # strings that look like ints are converted (casted) fine ('3', '2', 1.5), # floats work too but when casted to int they are rounded down! (8.2, 2, 4), (1, 2.9, 0.5), ]) def test_divide_numbers_good_inputs(numerator, denominator, expected): assert divide_numbers(numerator, denominator) == expected @pytest.mark.parametrize("numerator, denominator", [ # ignoring dict/set/list to keep it simple, those would actually # throw a TypeError when passed into int() (2, 's'), ('s', 2), ('v', 'w'), ]) def test_divide_numbers_raises_value_error(numerator, denominator): with pytest.raises(ValueError): divide_numbers(numerator, denominator) def test_divide_by_zero_does_not_raise_zero_division_exception(): assert divide_numbers(10, 0) == 0Passing the first assertion test is very easy. Just by adding the line
return int(numerator) / int(denominator)
to the divide_numbers()
function converts the string numbers into integers. So this is the basic function that I am starting with:
def divide_numbers(numerator, denominator): return int(numerator) / int(denominator)That passes the first unit test function with flying colours. Easy.
What I am struggling with is the following two assertion checks, specifically handling exceptions. For example when
('s', 2)
is passed into the function, since the first variable is a string and can't be converted to an int, I need to handle it by invoking an exception. I’ve made a few different attempts placing exceptions in different places and in different order. This is what I think is my strongest attempt so far:def divide_numbers(numerator, denominator): if denominator == 0: raise ZeroDivisionError('cannot divide by 0') elif numerator or denominator == str: raise ValueError('cannot be string') else: return int(numerator) / int(denominator)The first obvious problem I can already identify is at lines 4 and 5 because when ('3', '2', 1.5) are passed into the function, ‘3’ and ‘2’ are exited before reaching lines 6 and 7 and so are not converted into a string. To fix this issue I know I need to add further control with more conditionals but I can’t for the life of me find the right arrangement. That’s just one problem. The other problem is at lines 2 and 3. I am not raising the ZeroDivisionError properly. When I try change the
==
equality operator at line 2 to the =
assignment operator, I get a syntax error and none of the assertions even process, so the unit test fails completely. The first doc I found on Google related to zero division in Python was from a chapter titled Python “Programming / Exceptions” under the sub-section titled: “Catching exceptions”. It’s slightly outdated. Even though it’s written for Python 2, I found it to be easy to follow. To adapt the guide for Python 3, I referred to the official Python doc on porting Python 2 code to Python 3, specifically the section on “Capturing the Currently Raised Exception”. I also used Sebastina Raschka’s “The key differences between Python 2.7.x and Python 3.x with examples” on “Handling exceptions”. I also found a detailed guide on ZeroDivisionErrors titled “Python Exception Handling – ZeroDivisionError”. However it was verbose and hard to follow. I found Dan Bader + Said van de Klundert's guide on Python Exceptions to be enormously helpful as well.
What might you people recommend I explore next? Again, I am looking for tips and hints. Please do not complete the assignment for me.