Conversation
src/series_sum.py
Outdated
|
|
||
| def series_sum(n): | ||
| """Find the sum of the first nth terms of a series 1+1/4+1/7+...""" | ||
| sum = 0 |
There was a problem hiding this comment.
Bad variable name. Python has a built-in sum() function, and your editor should have highlighted it differently because of this fact. Typically when I'm trying to find the sum of something, I call the resultant value something like "total" or "result" just to avoid this.
src/series_sum.py
Outdated
| """Find the sum of the first nth terms of a series 1+1/4+1/7+...""" | ||
| sum = 0 | ||
| for i in range(n): | ||
| sum += float(1 / (1 + i * 3)) |
There was a problem hiding this comment.
In Python 2, this is integer division even if you use the float type object to turn it into a float after the fact. Thus, all your answers will be "1"
src/series_sum.py
Outdated
| sum = 0 | ||
| for i in range(n): | ||
| sum += float(1 / (1 + i * 3)) | ||
| return str("%.2f" % sum) |
There was a problem hiding this comment.
You're already using string formatting to turn your resultant total into a string. Why are you calling the str() type object to turn it into a string again?
| def test_series_sum(n, result): | ||
| """Test series_sum() for proper output in test cases.""" | ||
| from series_sum import series_sum | ||
| assert series_sum(n) == result |
There was a problem hiding this comment.
Your tests fail in Python 2 and you're not accounting for when n=0
Weekend Code-Wars Assignment.