Want more features on Pastebin? Sign Up, it's FREE!
Guest

Zeller's Algorithm (Python)

By: a guest on Jun 24th, 2013  |  syntax: Python  |  size: 4.29 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. from datetime import datetime
  3.  
  4. months = {"1":11, "2":12, "3":1, "4":2, "5":3, "6":4,
  5. "7":5, "8":6, "9":7, "10":8, "11":9, "12":10}
  6. days = {"1":"Monday", "2":"Tuesday","3":"Wednesday","4":"Thursday","5":"Friday","6":"Saturday","0":"Sunday"}
  7. bMonth = "0"
  8. bDay = "0"
  9. bYear = "0"
  10. bCent = "0"
  11. today = datetime.now()
  12. born = "You were born on a "
  13.  
  14.  
  15. validInput = False
  16. while (validInput == False):
  17.     while(len(bYear)!=2):
  18.         bYear = input("Want to know the day of the week you were born?\n Enter the two digit year: \n(e.g. if you were born in 1980 enter 80) YY ")
  19.     try:
  20.         tYear = int(bYear)
  21.         if tYear >= 0 and tYear <= 99:
  22.             validInput = True
  23.             bYear = int(bYear)
  24.     except ValueError:
  25.       bYear = input("You made an invalid entry. \nEnter the two digit year: \n(e.g. if you were born in 1980 enter 80) YY ")
  26.  
  27. validInput = False
  28. while (validInput == False):
  29.     while(len(bCent)!=2):
  30.         bCent = input("Enter the two digit century: \n(e.g. if you were born in 1980 enter 19) CC ")
  31.  
  32.     try:
  33.         tCent = int(bCent)
  34.         if tCent >= 0 and tCent <= 99:
  35.             validInput = True
  36.             bCent = int(bCent)
  37.     except ValueError:
  38.       bCent = input("You made an invalid entry. \nEnter the two digit century: \n(e.g. if you were born in 1980 enter 19) CC ")
  39.  
  40. validInput = False
  41. while (validInput == False):
  42.     while(len(bMonth)!=2):
  43.         bMonth = input("Enter a number for the month: MM ")
  44.  
  45.     try:
  46.         tMonth = int(bMonth)
  47.         if tMonth >= 1 and tMonth <= 12:
  48.             validInput = True
  49.             bMonth = int(bMonth)
  50.         else:
  51.             bMonth = "0"
  52.     except ValueError:
  53.       bMonth = input("You made an invalid entry. \nEnter a number for the month: MM ")
  54.  
  55. validInput = False
  56. while (validInput == False):
  57.     while(len(bDay)!=2):
  58.         bDay = input("Enter a number for the day: DD ")
  59.  
  60.     try:
  61.         tDay = int(bDay)
  62.         if bMonth == 1:
  63.             if tDay >= 1 or tDay <=31:
  64.                 validInput = True
  65.                 bDay = int(bDay)
  66.         elif bMonth == 2:
  67.             if bYear%4 == 0:
  68.                 if tDay >=1 or tDay <= 29:
  69.                     validInput = True
  70.                     bDay = int(bDay)
  71.             else:
  72.                 if tDay >=1 or tDay <= 28:
  73.                     validInput = True
  74.                     bDay = int(bDay)
  75.         elif bMonth == 3:
  76.             if tDay >= 1 or tDay <=31:
  77.                 validInput = True
  78.                 bDay = int(bDay)
  79.         elif bMonth == 4:
  80.             if tDay >= 1 or tDay <=30:
  81.                 validInput = True
  82.                 bDay = int(bDay)
  83.         elif bMonth == 5:
  84.             if tDay >= 1 or tDay <=31:
  85.                 validInput = True
  86.                 bDay = int(bDay)
  87.         elif bMonth == 6:
  88.             if tDay >= 1 or tDay <=30:
  89.                 validInput = True
  90.                 bDay = int(bDay)
  91.         elif bMonth == 7:
  92.             if tDay >= 1 or tDay <=31:
  93.                 validInput = True
  94.                 bDay = int(bDay)
  95.         elif bMonth == 8:
  96.             if tDay >= 1 or tDay <=31:
  97.                 validInput = True
  98.                 bDay = int(bDay)
  99.         elif bMonth == 9:
  100.             if tDay >= 1 or tDay <=30:
  101.                 validInput = True
  102.                 bDay = int(bDay)
  103.         elif bMonth == 10:
  104.             if tDay >= 1 or tDay <=31:
  105.                 validInput = True
  106.                 bDay = int(bDay)
  107.         elif bMonth == 11:
  108.             if tDay >= 1 or tDay <=30:
  109.                 validInput = True
  110.                 bDay = int(bDay)
  111.         elif bMonth == 12:
  112.             if tDay >= 1 or tDay <=31:
  113.                 validInput = True
  114.                 bDay = int(bDay)
  115.         else:
  116.             bDay = "0"
  117.     except ValueError:
  118.       bDay = input("You made an invalid entry. \nEnter a number for the day: DD ")
  119.  
  120. if today.year < int(str(bCent)+str(bYear)):
  121.     born = "You will born on a "
  122. A = int(months[str(bMonth)])
  123. B = bDay
  124. if A == 11 or A == 12:
  125.     if bYear == 00:
  126.         bYear = 99
  127.         bCent = bCent - 1
  128.     else:
  129.         bYear = bYear - 1
  130. C = bYear
  131. D = bCent
  132.  
  133. W = int((13*A - 1) / 5)
  134. X = int(C / 4)
  135. Y = int(D / 4)
  136. Z = int(W + X + Y + B + C - 2*D)
  137. R = int((Z%7))
  138.  
  139. if (R < 0):
  140.     R = (R+7)%6
  141.  
  142. print (born + days[str(R)])
clone this paste RAW Paste Data