Advertisement
Guest User

Zeller's Algorithm (Python)

a guest
Jun 24th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.11 KB | None | 0 0
  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.         bYear = int(bYear)
  21.         if bYear >= 0 and bYear <= 99:
  22.             validInput = True
  23.         else:
  24.             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 ")
  25.     except ValueError:
  26.       bYear = input("You made an invalid entry. \nEnter the two digit year: \n(e.g. if you were born in 1980 enter 80) YY ")
  27.  
  28. validInput = False
  29. while (validInput == False):
  30.     while(len(bCent)!=2):
  31.         bCent = input("Enter the two digit century: \n(e.g. if you were born in 1980 enter 19) CC ")
  32.  
  33.     try:
  34.         bCent = int(bCent)
  35.         if bCent >= 0 and bCent <= 99:
  36.             validInput = True
  37.         else:
  38.             bCent= input("Enter the two digit century: \n(e.g. if you were born in 1980 enter 19) CC ")
  39.     except ValueError:
  40.       bCent = input("You made an invalid entry. \nEnter the two digit century: \n(e.g. if you were born in 1980 enter 19) CC ")
  41.  
  42. validInput = False
  43. while (validInput == False):
  44.     while(len(bMonth)!=2):
  45.         bMonth = input("Enter a number for the month: MM ")
  46.  
  47.     try:
  48.         bMonth = int(bMonth)
  49.         if bMonth >= 1 and bMonth <= 12:
  50.             validInput = True
  51.         else:
  52.             bMonth = input("Enter a number for the month: MM ")
  53.     except ValueError:
  54.       bMonth = input("You made an invalid entry. \nEnter a number for the month: MM ")
  55.  
  56. validInput = False
  57. while (validInput == False):
  58.     while(len(bDay)!=2):
  59.         bDay = input("Enter a number for the day: DD ")
  60.  
  61.     try:
  62.         bDay = int(bDay)
  63.         if bMonth == 1:
  64.             if bDay >= 1 or bDay <=31:
  65.                 validInput = True
  66.         elif bMonth == 2:
  67.             if bYear%4 == 0:
  68.                 if bDay >=1 or bDay <= 29:
  69.                     validInput = True
  70.             else:
  71.                 if bDay >=1 or bDay <= 28:
  72.                     validInput = True
  73.         elif bMonth == 3:
  74.             if bDay >= 1 or bDay <=31:
  75.                 validInput = True
  76.         elif bMonth == 4:
  77.             if bDay >= 1 or bDay <=30:
  78.                 validInput = True
  79.         elif bMonth == 5:
  80.             if bDay >= 1 or bDay <=31:
  81.                 validInput = True
  82.         elif bMonth == 6:
  83.             if bDay >= 1 or bDay <=30:
  84.                 validInput = True
  85.         elif bMonth == 7:
  86.             if bDay >= 1 or bDay <=31:
  87.                 validInput = True
  88.         elif bMonth == 8:
  89.             if bDay >= 1 or bDay <=31:
  90.                 validInput = True
  91.         elif bMonth == 9:
  92.             if bDay >= 1 or bDay <=30:
  93.                 validInput = True
  94.         elif bMonth == 10:
  95.             if bDay >= 1 or bDay <=31:
  96.                 validInput = True
  97.         elif bMonth == 11:
  98.             if bDay >= 1 or bDay <=30:
  99.                 validInput = True
  100.         elif bMonth == 12:
  101.             if bDay >= 1 or bDay <=31:
  102.                 validInput = True
  103.         else:
  104.             bDay= input("Enter a number for the day: DD ")
  105.     except ValueError:
  106.       bDay = input("You made an invalid entry. \nEnter a number for the day: DD ")
  107.  
  108. if today.year < int(str(bCent)+str(bYear)):
  109.     born = "You will born on a "
  110. A = int(months[str(bMonth)])
  111. B = int(bDay)
  112. if A == 11 or A == 12:
  113.     if bYear == 00:
  114.         bYear = 99
  115.         bCent = bCent - 1
  116.     else:
  117.         bYear = bYear - 1
  118. C = bYear
  119. D = bCent
  120.  
  121. W = int((13*A - 1) / 5)
  122. X = int(C / 4)
  123. Y = int(D / 4)
  124. Z = int(W + X + Y + B + C - 2*D)
  125. R = int((Z%7))
  126.  
  127. if (R < 0):
  128.     R = (R+7)%6
  129.  
  130. print (born + days[str(R)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement