Jan-28-2020, 02:50 PM
The purpose of the code is to parse a text file (output from another program) which contains two date strings and a grade (string), check the interval between the two dates, and classify them accordingly. It also should prompt the user to recheck the dates if the interval exceeds 2 years.
The relevent code snippet is:
Thanks in advance.
The relevent code snippet is:
from datetime import datetime, timedelta #process values from textfile class readtextfile(object): def __init__(self,birth_date, exam_date,expiry_date,certification_status): self.birth_date = (birth_date) self.exam_date = (exam_date) self.expiry_date = (expiry_date) self.certification_status = certification_status @property def age_calculator(self): age = datetime.today()- self.birth_date return age @property def validity(self): return self.expiry_date - self.exam_date def restricted_reason(self): while True: if self.certification_status == "Fit" and self.validity < timedelta(days = 730): d = input("Please enter reason for shortened certificate. ") return d break elif self.certification_status == "Fit" and self.validity >= timedelta(days = 730): print("This is a normal certificate. ") break elif self.certification_status == "Limited" or self.certification_status == "Fail": e = input(f"Please enter reason for certifying as {self.certification_status}. ") return e break else: print("Does not compute!") g = input("Please enter certification status. ") self.certification_status = g continue def certificate_type(self): if self.certification_status == "Fit" and self.validity < timedelta(days = 730): return "Limited Time" elif self.certification_status == "Fit" and self.validity in range((timedelta(days = 730)), (timedelta(days = 732))): return "Normal" elif self.certification_status == "Limited": return "Limited" elif self.certification_status == "Fail": return "Fail" elif self.validity >= timedelta(days = 733): print("Please check expiry date - must not exceed 2 years.") new_date = input("Please enter expiry date in dd mm yyyy. ") self.expiry_date = new_date return readtextfile.restricted_reason() else: print("Does not compute!") return readtextfile.restricted_reason()When the code is run, I get:
Output: File "test.py", line 49, in certificate_type
elif self.certification_status == "Fit" and self.validity in range((timedelta(days = 730)), (timedelta(days = 732))):
TypeError: 'datetime.timedelta' object cannot be interpreted as an integer
Is there a way to convert the timedelta to integer (or float)?Thanks in advance.