I want to receive how many years/months/weeks/days in total of the number,
I want the output to say 732 is 2 years, 0 months, 0 weeks, 2 days but I would receive
"it has been: 2 Years , 24 Months , 105 Weeks , 732 Days Since Variable inputted"
*##variables / user input*
days = int(input("Enter Number of Days: "))
weeks = days / 7
months = days / 30.4
years = days / 365
*#if conditions here*
if days >= 7:
weeks = weeks + 1
elif weeks >= 4:
months = months + 1
elif months >= 12:
years = years + 1
*##print command for output*
print("it has been: ", int(years), "Years", ",", int(months)
, "Months", ",", int(weeks), "Weeks", ",", int(days), "Days","Since Variable inputted")
I know what I’m doing wrong just I don’t know the solution for it 😀
thank you for any informative answers
>Solution :
You don’t really need any if statements here. This is a more mathematical solution than the one above:
days = int(input("Enter Number of Days: "))
years = days // 365
months = (days - 365 * years) // 30.4
weeks = (days - (365 * years + 30.4 * months))//7
days = days - (365 * years + 30.4 * months + 7 * weeks)
print("it has been: ", int(years), "Years", ",", int(months), "Months", ",", int(weeks), "Weeks", ",", int(days), "Days","Since Variable inputted")
It’s just recurring subtraction/division.
Also, when dealing with more complicated projects, it can be useful to explain what you’ve done to try to fix it, your approach, exactly what you want, and since you said you knew what you were doing wrong, you are expected to say that if you have any information.