【发布时间】:2016-09-28 03:45:54
【问题描述】:
我正在编写一些代码,允许用户输入秒数,并接收它产生的天数、小时数、分钟数和秒数。但是,如果我输入任何大于 311039999 的数字,小时数将变为 24+,而不是 0。
现在我编写了一些程序,告诉用户如果超过上述值,这个数字太大了,但我想改变它,让它不再是问题。
这是我的代码:
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 >= 311040000:
print 'Your number is too large to calculate.'
elif user_sec >= 60 and user_sec < 3600:
print '{} seconds makes {} minute(s) and {} second(s).'.format(user_sec,tot_min_min,tot_min_sec)
elif user_sec >= 3600 and user_sec < 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 >= 86400 and user_sec < 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)
如果有帮助,我正在使用 Canopy。感谢您提供简单的答案,因为我只做了几个星期。
[编辑] 这是我的问题的一个例子。如果 user_sec = 1000000000,它会打印出“1000000000 秒等于 11574 天、77 小时、46 分钟和 40 秒。”我不确定数学问题出在哪里,但正确答案是“11574 天,1 小时 46 分 40 秒。”
【问题讨论】:
-
311040000 / 3600 = 86400,一天的秒数。这有帮助吗?
-
这和复数有什么关系?
-
标题中写着“大数字”。
标签: python complex-numbers canopy