Posts: 47
Threads: 14
Joined: Apr 2020
(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.
Posts: 1,838
Threads: 2
Joined: Apr 2017
Jun-27-2020, 07:13 PM
(This post was last modified: Jun-27-2020, 07:14 PM by ndc85430.)
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 .
Posts: 47
Threads: 14
Joined: Apr 2020
(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.
Posts: 1,838
Threads: 2
Joined: Apr 2017
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?
Posts: 47
Threads: 14
Joined: Apr 2020
(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.
Posts: 2,168
Threads: 35
Joined: Sep 2016
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.
Posts: 1,838
Threads: 2
Joined: Apr 2017
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.
Posts: 47
Threads: 14
Joined: Apr 2020
Jun-27-2020, 08:09 PM
(This post was last modified: Jun-27-2020, 08:09 PM by pav1983.)
(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.
Posts: 1,838
Threads: 2
Joined: Apr 2017
How would you do it with pen and paper? If you understand the steps, code should follow quite easily.
Posts: 47
Threads: 14
Joined: Apr 2020
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"
|