avatar Bite 186. Calculate number of books to have read at date ...

For this Bite you are asked to start working on a reading goal feature for PyBites My Reading List.

Code up get_number_books_read that takes a yearly # books to read int and then calculates the amount of books user should have read based on the at_date argument which defaults to NOW. So if the goal is 52, and we call this function week 11, it should return 11. If the goal is 100 and we call this function in week 47 the function should return 90 (47/52*100), rounding to int. Some more examples:

>>> get_number_books_read(100, 'Sunday, March 25th, 2019')
25
>>> get_number_books_read(52, 'Sunday, March 18th, 2019')
12
>>> get_number_books_read(52, '5-20-2018')
Traceback (most recent call last):
  File "", line 1, in 
  File "/Users/bbelderbos/code/bitesofpy/186/books.py", line 20, in get_number_books_read
    raise ValueError('Should have positive goal and future date')
ValueError: Should have positive goal and future date

Check the docstring, comments and tests for more guidance. For simplicity you can assume that a year has 52 weeks (hence WEEKS_PER_YEAR = 52). Hint: to get the week of the year, look into isocalendar. Keep calm and code in Python!

Login and get coding
go back Intermediate level
Bitecoin 3X

Will you be Pythonista #128 to crack this Bite?
Resolution time: ~37 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 4.0 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help