0

I am having trouble figuring out this pset. Every time, I use check50 it shows many red faces.

from datetime import date
import sys
import re
# pip imported library to convert numbers into word format
from num2words import num2words


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


def convert(dob):
    # Validates user input
    if re.search(r'^([1-2][0-9][0-9][0-9])-([0-1][0-9])-([0-3][0-9])$', dob):
        today = date.today()
        try:
            # Validate date format
            input_date = date.fromisoformat(dob)
        except ValueError:
            sys.exit("Invalid Date")
    # Calulations
    minus = today - input_date
    calculation = minus.days * 24 * 60
    word_form = num2words(calculation)

    # removes the word 'and'
    final = re.sub(r' and', '', word_form)

    return f'{final.capitalize()} minutes'

else:
    sys.exit("Invalid Date")


if __name__ == "__main__":
    main()

enter image description here

Edit: The indentation is my mistake while writing this form. My main problem is when using check50 and nothing is being outputted but, when I run the program locally it outputs the correct sentence.

2 Answers 2

0

I'm just a novice too. I think you need to briefly explain what you are trying to do. I'm suggesting you double-check your indentations and possibly input validations as well. Looks like you have a couple of indentation issues in the convert() block. You need to correct those and try running the code again. I suppose the try and except statements need to be properly aligned including anything within them. Also the if and the else block are not properly aligned (indented).

1
  • Thank you for the suggested and all is working well. The indentation were my mistake while writing this form. My main problem is when using check50 and nothing is being outputted but, when I run the program locally it outputs the correct sentence.
    – Ejay
    Commented Jan 13, 2023 at 0:31
0

Answer was to simply use the inflect library.

import inflect
p = inflect.engine()

You can install by using

pip install inflect

You must log in to answer this question.

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