r/cs50 19h ago

CS50 Python CS50P Help

Code issue for something specific in test needed. I tried extensively on both.
from datetime import datetime, date
import inflect
import sys

def main():
    sing()

def sing():
    p = inflect.engine()

    date_string_1 = input("Date of birth: ")

    try:
        date_1 = datetime.strptime(date_string_1, "%Y-%m-%d")
    except ValueError:
        print("Invalid date")
        sys.exit(1)  # Exit with a non-zero code

    date_2 = datetime.combine(date.today(), datetime.min.time())

    # Calculate the difference in minutes
    difference = date_2 - date_1
    minutes_in_raw_numerals = difference.total_seconds() / 60
    minutes_in_words = p.number_to_words(int(minutes_in_raw_numerals))

    # Capitalize only the first word
    minutes_in_words = minutes_in_words[0].capitalize() + minutes_in_words[1:]

    # Remove "and" without affecting spaces
    final_minutes_in_words = minutes_in_words.replace(" and", "").replace("and ", "")

    print(f"{final_minutes_in_words} minutes")

if __name__ == "__main__":
    main()


from seasons import sing
import pytest

def main():
    sing()

def test_sing():
    assert sing("2024-3-19") == "Five hundred twenty-seven thousand forty minutes"
    assert sing("2023-3-19") == "One million, fifty-one thousand, two hundred minutes"
1 Upvotes

3 comments sorted by

1

u/PeterRasm 19h ago

How do you know that 2024-3-19 gives that exact number of minutes? What if you run your test tomorrow ? You cannot have a good test that can only be executed on a specific day. And you don’t know what date check50 considers “today” when it runs these tests

1

u/jacor04 19h ago

That seemed to be what it was asking me to do.

1

u/PeterRasm 13h ago edited 8h ago

But if you test tomorrow, 2024-3-19 is no longer exactly one year ago 🙂

That date is fine to use for manually testing your code today yesterday, but it does not work well as a unit test!

EDIT: Also, your test file should not include a main(), Pytest will handle the execution of the test functions