Python Forum
How to calculate the year by adding n months?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to calculate the year by adding n months?
#11
(Jun-27-2020, 07:06 PM)ndc85430 Wrote: 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.

There are 12 months in a year. I put in there year = month * 12 but that didn't work either.
Reply
#12
If there are 12 months in a year, why do you think you need to multiply your number of months by 12? If month has a value of 24, then as you've written it, year ends up being 144. which is of course wrong. Perhaps your variable name is confusing you; it really should be months.
Reply
#13
(Jun-27-2020, 07:13 PM)ndc85430 Wrote: If there are 12 months in a year, why do you think you need to multiply your number of months by 12? If month has a value of 24, then as you've written it, year ends up being 144. which is of course wrong. Perhaps your variable name is confusing you; it really should be months.

The reason it is month and not months is because the site gives you the arguments and makes you use them as is. Now I know in a for loop I've seen variable names get an 's' added to the end of them.
Reply
#14
OK, fair enough. This calculation is really really simple though. If you have 24 months, how many years does that correspond to? What about 30?
Reply
#15
(Jun-27-2020, 07:20 PM)ndc85430 Wrote: OK, fair enough. This calculation is really really simple though. If you have 24 months, how many years does that correspond to? What about 30?

I completely agree that the calculations are simple. 24 months is 2 years. 30 months would still be two years because it's before 36. I know my math.

I just don't know how to apply it to solve this problem. That's all I was trying to say before. And I haven't been working on the problem for two weeks straight. I just haven't done much with it until today.
Reply
#16
Use
https://docs.python.org/3/glossary.html#...r-division Wrote:floor division
Mathematical division that rounds down to nearest integer. The floor division operator is //. For example, the expression 11 // 4 evaluates to 2 in contrast to the 2.75 returned by float true division. Note that (-11) // 4 is -3 because that is -2.75 rounded downward. See PEP 238.
Reply
#17
If you know these facts, then the problem is pretty trivial to solve. Look at the first example you gave:

after_n_months(2020, 24) ➞ 2022

Given what you said above, how do you not know how to compute the result?! I'm confused.
Reply
#18
(Jun-27-2020, 07:31 PM)ndc85430 Wrote: If you know these facts, then the problem is pretty trivial to solve. Look at the first example you gave:

after_n_months(2020, 24) ➞ 2022

Given what you said above, how do you not know how to compute the result?! I'm confused.

I'm confused also. That's why I'm on here. I don't know how to set up the code for this particular problem. I don't understand what I'm missing here. And that example was from the tests they give that go with the problem.
Reply
#19
How would you do it with pen and paper? If you understand the steps, code should follow quite easily.
Reply
#20
Sorry if I drove you crazy. I really do appreciate it that you took the time to try and help me. Your help is always appreciated.

def after_n_months(year, month):
  try:
    return year + (month // 12) 
  except:
    if year is None:
      return "year missing"
    elif month is None:
      return "month missing"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to calculate a months' 1st, 4th, 7th day and also 1st again? cananb 1 1,564 Nov-11-2020, 09:17 PM
Last Post: deanhystad
  how to tell python to do something every 6 months Jezani_VIII 1 1,983 Aug-18-2019, 02:30 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020