![]() |
Calendar calcualtions - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Calendar calcualtions (/thread-3100.html) Pages:
1
2
|
Calendar calcualtions - deusvult - Apr-28-2017 Hi! I am trying to calculate difference in days between two dates. If the dates are close enough the answer is correct but as the difference increases I start getting an error. I know there is a different way how to do it, but I would appreciate help in spotting whats wrong in this one. print "***Calendar simulator 2.0***" print d1,m1,y1 = map(int,raw_input("Enter start date in DD.MM.YYYY format: ").split(".")) print d2,m2,y2 = map(int,raw_input("Enter end date in DD.MM.YYYY format: ").split(".")) print m111=m1-1 m222=m2-1 y11 = int((y1-1)/4.)+(y1-1)*365 if (y1)/4. == float(int((y1)/4.)): a = [31,29,31,30,31,30,31,31,30,31,30,31] else: a = [31,28,31,30,31,30,31,31,30,31,30,31] m11 = sum(a[0:m111]) x = y11+m11+d1 y22 = int((y2-1)/4.)+(y2-1)*365 if (y2)/4. == float(int((y2)/4.)): b = [31,29,31,30,31,30,31,31,30,31,30,31] else: b = [31,28,31,30,31,30,31,31,30,31,30,31] m22 = sum(b[0:m222]) y = y22+m22+d2 diff = abs(y-x) s = (diff % 10) if s == 1: print "Difference is: ", diff, " day." print print "Ready." else: print"Difference is: ", diff, " days." print print "Ready." print x print y RE: Calendar calcualtions - wavic - Apr-28-2017 Hello and welcome! Turn each date in epoch time, subtract them and turn the result into days. RE: Calendar calcualtions - deusvult - Apr-28-2017 (Apr-28-2017, 10:06 PM)wavic Wrote: Hello and welcome! Thanks for suggestion, but that's not the aim of the task. I am trying to make it work using math and cannot spot the mistake :( RE: Calendar calcualtions - wavic - Apr-28-2017 It's hard to follow the code - the variable names are meaningless. I don't know what I am looking at all the time. RE: Calendar calcualtions - volcano63 - Apr-28-2017 You account for the possibility that a year in question may be a leap year - it's a little bit more complicated than you assume, but you don't account for the fact that there may be leap years in between. RE: Calendar calcualtions - deusvult - Apr-28-2017 (Apr-28-2017, 10:11 PM)wavic Wrote: It's hard to follow the code - the variable names are meaningless. I don't know what I am looking at all the time. I was rushing it and it is almost my first code. If you look into it its really no that confusing, but I guess ill look for help from someone else then. Thanks for the effort anyways RE: Calendar calcualtions - zivoni - Apr-28-2017 From quick look it seems that you count every year divisible by four as a leap year; thats not right - for example years 1900 or 2100 are not leap years. As you do exactly same calculations for both dates, it would make sense to rewrite it as a function. RE: Calendar calcualtions - wavic - Apr-28-2017 (Apr-28-2017, 10:39 PM)deusvult Wrote:(Apr-28-2017, 10:11 PM)wavic Wrote: It's hard to follow the code - the variable names are meaningless. I don't know what I am looking at all the time. I could but I am sick. My brain is liquified. RE: Calendar calcualtions - Ofnuts - Apr-30-2017 (Apr-28-2017, 10:39 PM)deusvult Wrote:(Apr-28-2017, 10:11 PM)wavic Wrote: It's hard to follow the code - the variable names are meaningless. I don't know what I am looking at all the time. If it were not confusing you would understand it and not be here for help. Your code is hard to read and even though you don't notice, it impacts *your* ability to understand it. Put descriptive names on variables and you'll have a very different view. Also add comment explaing what the intent of the code is. PS: To test that a year is a multiple of 4: year % 4 == 0 is a bit less convoluted than (y1)/4. == float(int((y1)/4.)) (and as some have remarked even done properly this isn't sufficient to determine leap-ness).
RE: Calendar calcualtions - Larz60+ - May-01-2017 Here is an excellent resource for all sorts of date and time calculations: http://pleac.sourceforge.net/pleac_python/datesandtimes.html |