Advertisement
luisnaranjo733

day of the week

Feb 20th, 2012
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. """
  2. The program should take three arguments. The first will be a day, the second will be month, and the third will be year. Then, your program should compute the day of the week that date will fall on.
  3. """
  4.  
  5. import sys
  6. import calendar
  7.  
  8. months = {
  9. 'january':1,
  10. 'february':2,
  11. 'march':3,
  12. 'april':4,
  13. 'may':5,
  14. 'june':6,
  15. 'july':7,
  16. 'august':8,
  17. 'september':9,
  18. 'october':10,
  19. 'november':11,
  20. 'december':12
  21. }
  22.  
  23. weekday = {
  24. 0:'monday',
  25. 1:'tuesday',
  26. 2:'wednesday',
  27. 3:'thursday',
  28. 4:'friday',
  29. 5:'saturday',
  30. 6:'sunday',
  31. }
  32.  
  33. def get_args():
  34.     "Returns a dict of args"
  35.     if len(sys.argv) <= 3:
  36.         print "Not enough arguments!"
  37.         print "I need:"
  38.         print "\tThe day [1]"
  39.         print "\tThe month [2]"
  40.         print "\tThe year [3]"
  41.         sys.exit(0)
  42.        
  43.     try:
  44.         month = months[sys.argv[2].lower()]
  45.     except:
  46.         try:
  47.             month = int(sys.argv[2])
  48.         except:
  49.             month = months[sys.argv[2]]
  50.            
  51.     return {'day': int(sys.argv[1]),'month':month,'year':int(sys.argv[3])}
  52.        
  53. def main(year,month,day):
  54.     int_day = calendar.weekday(year,month,day)
  55.     print weekday[int_day]
  56.    
  57. if __name__ == "__main__":
  58.     date = get_args()
  59.     year = date['year']
  60.     month = date['month']
  61.     day = date['day']
  62.     main(year,month,day)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement