0

I made a birthday program in Python, the program ask from the user the month he were born in, and then calculate the months until the user have birthday.

For example, the user enter the number 5 and the current month is 2, the program output "You have 3 months until your birthday!".

I don't know how to calculate the months until he have birthday correct.

Example:

from datetime import datetime

def Birthday():
    CurrentMonth = datetime.now().month
    BornIn = input("What month were you born in ? - ")

    result = int(BornIn) + CurrentMonth
    if int(BornIn) == CurrentMonth:
        print("You have already Birthday in this month!")
    elif int(BornIn) > 12:
        print("Invalid Input!")
    else:
        print("You have", result , "monthes until your Birthday!")



Birthday()

What mathematical action do I need to do, to calculate the months until his birthday?

Look at line 7, I used + to calculate but obviously it won't work.

Edit:

I need to do result = CurrentMonth - int(BornIn). This should fix the problem.

nakE
  • 362
  • 1
  • 13
  • 1
    Does this answer your question? [Best way to find the months between two dates](https://stackoverflow.com/questions/4039879/best-way-to-find-the-months-between-two-dates) – Sayse Dec 20 '19 at 09:44
  • "What mathematical action do I need to do" Well, `+` is addition. You need subtraction... so `5 - 2`, by the sounds of it, if you just want a simplistic subtraction of months (without taking into account the day of the month etc). https://www.digitalocean.com/community/tutorials/how-to-do-math-in-python-3-with-operators will help you implement it in python. I can't work out if this is actually a python question or just an infant-school maths question about the difference between addition and subtraction. – ADyson Dec 20 '19 at 09:45
  • @ADyson Oh you right, I need to do CurrentMonth - BornIn. Thanks. – nakE Dec 20 '19 at 09:48

1 Answers1

1

Your operation is not correct :

You should do:

result = (int(BornIn) - CurrentMonth)%12

Modulo is here to manage case when your birthday is in the next year. Without modulo you will have negative results (here you won't have any problem because we are in December so your birthday can't be in the 13th months)

Clément
  • 1,128
  • 7
  • 21
  • I cant just do `result = CurrentMonth - int(BornIn)` ? – nakE Dec 20 '19 at 09:54
  • @nakE try `(2 - 5)` and you get `-3` (it was 3 month ago) but you need `9` (in next year) and using `%12` you get `9` – furas Dec 20 '19 at 09:57
  • Check at the edit I just made, and replace current month by 6. You can't have any problem in december since you can't have negative result – Clément Dec 20 '19 at 09:59
  • @Clément Why should I replace CurrentMonth with 6 ? No matter I understand what you mean by this. – nakE Dec 20 '19 at 10:05
  • Imagine you are born in January and current month is June. So your next birthday is in 7 months. Without modulo you would have `1 - 6 = -5`. With modulo you will have `(1 - 6) % 12 = 7`. And yes, sorry you can have this happens in December so don't replace anything. But in january you can't have negative value so it wouldn't be representative of all possible results. Because 1-x with x from 1 to 12 can't be negative. – Clément Dec 20 '19 at 10:18