Python having issues computing large numbers in complex equations
<p>I'm programming some code that allows a user to input seconds, and receive how many days, hours, minutes, and seconds it churns out to. However, if I enter any number larger than 311039999, the amount of hours goes to 24+, instead of 0.</p>

<p>Right now I have something programmed in that tells the user that the number is too large if it exceeds the aforementioned value, but I want to change it so that it's not a problem anymore.</p>

<p>Here is my code:</p>

<pre><code>user_sec= int(input("How many seconds are there? "))
#When max value is minutes, displays number of minutes
tot_min_min = user_sec/60
#When max value is minutes, displays number of seconds
tot_min_sec = user_sec%60

#When max value is hours, displays number of hours
tot_hr_hr = user_sec/3600
#When max value is hours, displays number of minutes
tot_hr_min = tot_min_min%60
#When max value is hours, displays number of seconds
tot_hr_sec = user_sec%60

#When max value is days, displays number of days
tot_day_day = user_sec/86400
#When max value is days, displays number of hours
tot_day_hr = tot_hr_hr/3600
#When max value is days, displays number of minutes
tot_day_min = tot_hr_min%60
#When max value is days, displays number of seconds
tot_day_sec = user_sec%60

if user_sec &gt;= 311040000:
    print 'Your number is too large to calculate.'
elif user_sec &gt;= 60 and user_sec &lt; 3600:
        print '{} seconds makes {} minute(s) and {} second(s).'.format(user_sec,tot_min_min,tot_min_sec)
elif user_sec &gt;= 3600 and user_sec &lt; 86400:
        print '{} seconds makes {} hour(s), {} minute(s) and {} second(s).'.format(user_sec,tot_hr_hr,tot_hr_min,tot_hr_sec)
elif user_sec &gt;= 86400 and user_sec &lt; 311040000:
        print '{} seconds makes {} days(s), {} hour(s), {} minute(s) and {} second(s).'.format(user_sec,tot_day_day,tot_day_hr,tot_day_min,tot_day_sec)
else:
    print 'There is/are {} second(s).'.format(user_sec)
</code></pre>

<p>I'm using Canopy, if this helps. Simple answers are appreciated, since I've only been doing this for a few weeks.</p>

<p>[EDIT] Here's an example of my problem. If user_sec = 1000000000, it prints out '1000000000 seconds makes 11574 days(s), 77 hour(s), 46 minute(s) and 40 second(s).' I'm not sure where the mathematical issue is, but the correct answer is '11574 days, <strong><em>1</em></strong> hour, 46 minutes and 40 seconds.'</p>

<p>If you don't mind using a 3rd party module, <code>dateutil</code> provides an easy way to do this:</p>

<pre><code>from dateutil.relativedelta import relativedelta

user_sec = int(input("How many seconds are there? "))
d = relativedelta(seconds=user_sec)
print(d)
</code></pre>

<p>This will output the following if you enter <code>351080000</code></p>

<pre><code>relativedelta(days=+4063, hours=+10, minutes=+13, seconds=+20)
</code></pre>

<p>From there you can print out a more user friendly string:</p>

<pre><code>print('{0} seconds makes {1.days} days(s), {1.hours} hour(s), {1.minutes} minute(s) and {1.seconds} second(s).'.format(user_sec, d))

351080000 seconds makes 4063 days(s), 10 hour(s), 13 minute(s) and 20 second(s).
</code></pre>

<hr>

<p>Otherwise it is pretty straightforward to calculate the days. hours, minutes and seconds:</p>

<pre><code>n = user_sec
days, n = divmod(n, 86400)
hours, n = divmod(n, 3600)
minutes, n = divmod(n, 60)
seconds = n

print('{} seconds makes {} days(s), {} hour(s), {} minute(s) and {} second(s).'.format(user_sec, days, hours, minutes, seconds))
</code></pre>

python complex-numbers canopy