Take the 2-minute tour ×

I am a beginner programmer and here is my program so far:

    def getYearsandDays():  
        c = eval(input("Enter a number: "))  
        d = c // 1  
        e = (c - d) * 365  
        f = e // 1  
        return f,d  
        print(d , "years and", f, "days")  


For example, suppose that c is 1.34. Converting that to an integer gives you 1 year = d . Now 1.34 - 1 gives you .34. Multiplying that by 356 gives you 124.1 = e . Making that an integer gives you 124 days = f. So 1.34 years is 1 year(d) and 124 days(f).

I already know my print function is wrong because I'm stuck as to how would I get outputs like this:

6 years and 1 day  
1 year and 137 days  
67 days  

Instead of:

6 years and 1 days  
1 years and 137 days  
0 years and 67 days  

I'm guessing I might have to convert my integers back to strings and make If-Then statements, but I'm not 100% sure.

share|improve this question
This code won't work because you have a print after the return. What does the () on the last line mean? –  user647772 Feb 27 '12 at 20:53

2 Answers 2

This is what I would do:

def years_and_days():
    # Use input instead of raw_input if you're using Python 3.x
    time = float(raw_input('Enter a number: '))
    years = int(time)
    days = int((time - int(time)) * 365)
    if years:
        print years, 'years' if years > 1 else 'year',
    if days:
        print days, 'days' if days > 1 else 'day'


>>> years_and_days()
Enter a number: 3
3 years
>>> years_and_days()
Enter a number: 1.34
1 year 124 days
>>> years_and_days()
Enter a number: 0.32
116 days
share|improve this answer

if c is your input: you could do c MOD 1...that would give you the decimal.
To get the integer you just need c-cmod1...the rest of your calculations should work then...

mod is short for modulus - which is basically a way to calculate the remainder of something. so 5mod2 is the remainder of 5/2 or 1.

Not sure what language you're using, but if you search for modulus and the name of your language in google you'll be able to find what you're looking for.

Hope that helps

def getYearsandDays():  
        d = c - c%1 //years
        e = (c -d) * 365  //days in decimal format
        f = e - e%1 // days in integer format...you probably would get away with just rounding here too... 
        return f,d  

share|improve this answer
BTW just realized this is python. Use the % symbol for modulus in this example. –  SimaPro Feb 27 '12 at 23:25

Your Answer


