2

My code is functional and passes all of the CS50 tests except for the last one. What could be causing this?

This is my main code:

from datetime import timedelta
import inflect
import sys

def main():
    print(convert(input("Date of Birth: ")))


def convert(x):
    q = inflect.engine()
    try:
        years, months, days = x.split("-")
        years = int(years)
        months = int(months)
        days = int(days)
        dob = date(years, months, days)
        today = date.today()
        difference = today - dob
        min = round(difference.total_seconds()/60)
        words = q.number_to_words(min, andword="").capitalize() + " minutes"
        return words
    except ValueError:
        sys.exit("Date must be inputted in YYYY-MM-DD format.")



if __name__ == "__main__":
    main()

This is my test code:

from seasons import convert
import pytest

def test_seasons():
    assert convert("2022-09-10") == "Five hundred twenty-five thousand, six hundred minutes"
    assert convert("2021-09-10") == "One million, fifty-one thousand, two hundred minutes"
    with pytest.raises(SystemExit):
        convert("sdfs-sd-sd")
    with pytest.raises(SystemExit):
        convert("20220910")
    with pytest.raises(SystemExit):
        convert("cat")

And when I run check50 this is what I get:

:) seasons.py and test_seasons.py exist
:) Input of "1999-01-01" yields "Five hundred twenty-five thousand, six hundred minutes" when today is 2000-01-01
:) Input of "2001-01-01" yields "One million, fifty-one thousand, two hundred minutes" when today is 2003-01-01
:) Input of "1995-01-01" yields "Two million, six hundred twenty-nine thousand, four hundred forty minutes" when today is 2000-01-1
:) Input of "2020-06-01" yields "Six million, ninety-two thousand, six hundred forty minutes" when today is 2032-01-01
:) Input of "1998-06-20" yields "Eight hundred six thousand, four hundred minutes" when today is 2000-01-01
:) Input of "February 6th, 1998" prompts program to exit with sys.exit
:( seasons.py passes all checks in test_seasons.py
    expected exit code 0, not 1

2 Answers 2

1

So for anybody else who runs into this problem. The solution appears to be that the test code relied on a hardcoded date as an input, which was one year from the current date at the time of programming, and the program used the current date in order to do the calculations and arrive at an output.

What this means is that if I was to wait until tomorrow for example, my tests would fail.

In order to fix this, I modified my code to take in two variables: one for the current date, and one for the date of birth; then, in my test file I made those two dates dependent on one another like so:

from seasons import convert
from datetime import date
from datetime import timedelta
import pytest

def test_convert():
    assert convert(str(date.today()-timedelta(days=365)), date.today()) == "Five hundred twenty-five thousand, six hundred minutes"
    assert convert(str(date.today()-timedelta(days=730)), date.today()) == "One million, fifty-one thousand, two hundred minutes"
    with pytest.raises(SystemExit):
        convert("cat", date.today())
    with pytest.raises(SystemExit):
        convert("2024-09-10", date.today())
    with pytest.raises(SystemExit):
        convert("2022-13-10", date.today())
    with pytest.raises(SystemExit):
        convert("2022-13-35", date.today())
    with pytest.raises(SystemExit):
        convert("bird-sa-sa", date.today())
    with pytest.raises(SystemExit):
        convert(" 2022-13-12", date.today())

I chose to just use the datetime library's functionality in order to do this, but I imagine the same result could be achieved by hard coding both the current date and the date of birth; however, if I was going to take that path I would have to modify my program furter, because the input for the current date was based on a datetime object not a string:

today = date.today()
        difference = today - dob

Hope this helps someone!

1

Instead of changing the implementation of your convert function to accept two arguments, you could have changed your test like this:

from seasons import convert
from datetime import date, timedelta
import pytest


def test_convert():
    assert convert(str(date.today() - timedelta(days=365))) == "Five hundred twenty-five thousand, six hundred minutes"
    assert convert(str(date.today() - timedelta(days=2*365))) == "One million, fifty-one thousand, two hundred minutes"
    with pytest.raises(SystemExit):
        convert("1990-12-123")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .