Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

new to coding, need if statement help on my own little code I made

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading