Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions