0

I am composing a program which allows me to calculate a person's age in days- accounting for every day including leap days, and irregular number of days in months. Here is a line of code:

number_of_days = (((year2 - year1) * 12) + (month2 - month1)) * 30.4375 + (day2 - day1)

I got the value 30.4375 by dividing 365.25 by 12.

Now I only need to store the integer part in number_of_days. How do I do that?

Help is really appreciated!

P.S. All variable values are int values

1
  • The correct answer is to use a cast to int(). However, when you attempt this, you are getting an error as though you are attempting to assign a value to the int() function. Show us more of the surrounding code (when you try int()), and we'll find your issue. Commented Apr 20, 2013 at 22:46

5 Answers 5

4

You can either use int:

number_of_days = int((((year2 - year1) * 12) + (month2 - month1)) * 30.4375 + (day2 - day1))

or math.floor:

import math

number_of_days = math.floor((((year2 - year1) * 12) + (month2 - month1)) * 30.4375 + (day2 - day1))

But then number_of_days will be a float, so I think the first solution is better.

Sign up to request clarification or add additional context in comments.

5 Comments

Thats great! But only the integer part in required. Floor function is returning a float value. Also the second method doesn't seem to work it is giving an error of invalid syntax.
yup- its giving an error of invalid syntax- I apologize in advance if I have done something silly; I am learning python at the moment: The error returned is: int: ^ SyntaxError: invalid syntax
Show us what you are doing that is resulting in the syntax error.
This should be: number_of_days = int((((year2 - year1) * 12) + (month2 - month1)) * 30.4375 + (day2 - day1))
Thanks soo much everyone! You guys helped me out!
3

Just call int:

number_of_days = int((((year2 - year1) * 12) + (month2 - month1)) * 30.4375 + (day2 - day1))

2 Comments

@AbrarKhan Print more of your code, your syntax sounds like it is having trouble.
Hey! Thanks so much! Fixed it. I sincerely thank you for great advice thanks!
2

consider to use round instead of converting that is more accurate:

import math
int(math.round((((year2 - year1) * 12) + (month2 - month1)) * 30.4375 + (day2 - day1)))

7 Comments

That is very specific to the intention of the application. If I am 25 years and 300 days old, I don't say that I am 26 years old. I say that I am 25 (even though rounding would lead me to 26).
Hey! Thats a great idea! But would it return an int value?
@BlackVegetable I say I am 28 when I am 27 and 8 months old
@0x90 I stand corrected! That's what I get for making assumptions about everyone else!
Hey yeah! It prints int values now :) Thanks sooo much Guys! I sincerely thank you all :)
|
2

Could you just use the floor function?

number_of_days = math.floor((((year2 - year1) * 12) + (month2 - month1)) * 30.4375 + (day2 - day1))

1 Comment

math.floor will store it as 30.0, which can be irritating.
2

If your goal is to calculate the number of days between dates you can use the datetime module:

import datetime
birth = datetime.date(1967,11,14)
today = datetime.date.today()

number_of_days = (today - birth).days

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.