Program to calculate the number of days in python


SUBMITTED BY: Guest

DATE: Dec. 9, 2013, 3:30 a.m.

FORMAT: Text only

SIZE: 2.7 kB

HITS: 839

  1. # A python script to calculate the number of days
  2. # since your date of birth.
  3. # Solved it as a assignment problem from the Udacity course site
  4. # Though the solution is a lot different from theirs
  5. def isleap(year):
  6. if year%400==0:
  7. return True
  8. elif year%100==0: #got from wikipedia...the code for leap year
  9. return False
  10. elif year%4==0:
  11. return True
  12. else:
  13. return False
  14. def daysleft(year,month,day):
  15. if isleap(year):
  16. daysOfMonths = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  17. else:
  18. daysOfMonths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  19. x=daysOfMonths[month-1]-day
  20. for i in range(month,12):
  21. x=x+daysOfMonths[i]
  22. return x
  23. def daysbefore(year,month,day):
  24. if isleap(year):
  25. return 366-daysleft(year,month,day)
  26. else:
  27. return 365-daysleft(year,month,day)
  28. def daysBetweenDates(year1, month1, day1, year2, month2, day2):
  29. if year1==year2:
  30. return abs(daysleft(year1,month1,day1)-daysleft(year2,month2,day2))
  31. else:
  32. first=daysleft(year1,month1,day1)
  33. last=daysbefore(year2,month2,day2)
  34. middle=0
  35. while year1+1<year2:
  36. middle+=daysleft(year1+1,1,0)
  37. year1+=1
  38. return first+middle+last
  39. def mykalakari():
  40. print "Hey Prasanth!!!"
  41. query=raw_input("Want to know how long you were living since your birthday in terms of days?\
  42. If yes then type y or type n if not interested ")
  43. if query=='y':
  44. year1=int(raw_input("Enter your birthday year eg. 1947 "))
  45. month1=int(raw_input("Enter your month eg. 8 for september "))
  46. day1=int(raw_input("Enter your date "))
  47. year2=int(raw_input("Enter todays year "))
  48. month2=int(raw_input("Enters todays month "))
  49. day2=int(raw_input("Enters todays date "))
  50. print("The number of days you are a burden to this world :P is ")
  51. print daysBetweenDates(year1, month1, day1, year2, month2, day2)
  52. print "Wow!!! You sure are growing old"
  53. a=raw_input("type something to exit")
  54. elif query=='n':
  55. print "You are fit for nothing...I wasted so much of my time to write this code for you....and all \
  56. you do is ignore it...you"
  57. a=raw_input("type something to exit")
  58. else:
  59. print "I didn't understand what you typed..."
  60. print "repeating again"
  61. mykalakari()
  62. mykalakari()

comments powered by Disqus