Question: please modify the following python code to fix the logic errors: from datetime import datetime def calculate _ duration ( date _ str ) :

please modify the following python code to fix the logic errors: from datetime import datetime
def calculate_duration(date_str):
try:
input_date = datetime.strptime(date_str,'%m/%d/%Y')
except ValueError:
return "Invalid date format. Please enter date in MM/DD/YYYY format."
current_date = datetime.now()
duration = current_date - input_date
years = duration.days //365
months =(duration.days %365)//30
weeks =(duration.days %365)%30//7
days =(duration.days %365)%30%7
result = f"{years} years, {months} months, {weeks} weeks, and {days} days ago."
return result
date_input = input("Enter a date (MM/DD/YYYY): ")
print(calculate_duration(date_input))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!