Python Forum
How to calculate the year by adding n months? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to calculate the year by adding n months? (/thread-27933.html)

Pages: 1 2


How to calculate the year by adding n months? - pav1983 - Jun-27-2020

Hi Everyone,

I've successfully solved many problems. I'm even starting to solve some in the 'easy' category on Edabit. However, this 'very easy' problem seems harder than the category says. I'm really lost and don't know where to begin. Here is the problem:

Create a function that takes in year and month as input, then return what year it would be after n-months has elapsed.

Examples
after_n_months(2020, 24) ➞ 2022

after_n_months(1832, 2) ➞ 1832

after_n_months(1444, 60) ➞ 1449
Notes
Assume that adding 12 months will always increment the year by 1.
If no value is given for year or months, return "year missing" or "month missing".
At least one value will be present.



RE: After N Months - ndc85430 - Jun-27-2020

What is causing you difficulty? Given a year and a number of months, how would you work out what the new year should be? It's a pretty trivial problem.


RE: After N Months - pav1983 - Jun-27-2020

(Jun-27-2020, 06:13 PM)ndc85430 Wrote: What is causing you difficulty? Given a year and a number of months, how would you work out what the new year should be? It's a pretty trivial problem.

def after_n_months(year, month):
	if month % 12 == 0:
		year += 1
	return year
This is what I have so far and it's VERY CLOSE.


RE: How to calculate the year by adding n months? - Yoriz - Jun-27-2020

calculate how many full years n months is and then add it to years


RE: How to calculate the year by adding n months? - ndc85430 - Jun-27-2020

Express in words what you're checking on line 2. Does it make sense to increment year by 1 whenever that is true?


RE: How to calculate the year by adding n months? - pav1983 - Jun-27-2020

(Jun-27-2020, 06:26 PM)ndc85430 Wrote: Express in words what you're checking on line 2. Does it make sense to increment year by 1 whenever that is true?

I thought it was because 12 months = 1 year.


RE: How to calculate the year by adding n months? - Yoriz - Jun-27-2020

your code at the moment would only add 1 year whenever months is divisible by 12.
ie 12, 24, 36, 48 would all add just 1 year, any number of months that was not a full year would add nothing.


RE: How to calculate the year by adding n months? - ndc85430 - Jun-27-2020

Well, remember that % gives you the remainder after division, so any multiple of 12 will satisfy that condition.


RE: How to calculate the year by adding n months? - pav1983 - Jun-27-2020

(Jun-27-2020, 06:50 PM)ndc85430 Wrote: Well, remember that % gives you the remainder after division, so any multiple of 12 will satisfy that condition.

OK. I get what you guys are saying about the divisible by 12. I understand that it will only increment by 1 each time it is divisible by 12, which would not be correct. I just don't know how to increment the years based on the amount of months. That's where I'm still stuck and confused. I know this may seem easy to you guys because you're experts, but I've been stuck on this problem for weeks. The resources they provide with this problem don't help either.


RE: How to calculate the year by adding n months? - ndc85430 - Jun-27-2020

How many months are in a year? Given a number of months, how can you work out how many years that corresponds to? This really isn't something that should take you weeks, nor is something an expert can only do; it's a basic calculation.