Jump to content


Check out our Community Blogs

elcentro3m

Member Since 12 Mar 2013
Offline Last Active May 14 2013 07:08 PM
-----

Posts I've Made

In Topic: Python - Calendar - Input, Error Catching, While Loops

14 May 2013 - 03:14 AM

Update:

Added a couple more options.
 

#!/usr/bin/python3
#Terminal Syntax: python3 cal_choose4.py
#Python 3.2
#cal_choose4.py

import calendar

while True:

#multi-line option
#      choice = input("Please enter\n\n 1 (Year)\n 2 (Month)\n 3 (Day of Week)\n 4 (Date/Time Math)\n 5 to Quit'\n\n: ")
      
#single line option      
      choice = input("Please enter '1 (Year), 2 (Month), 3 (Day of Week), 4 (Date/Time Math), or 5 to Quit': ")

      if choice == '1':

        #Generate a Calendar Year from user input

        year = input("\n Year (Ex: 2013): ")
        print("\n Here it is: \n")
        print((calendar.calendar(int(year))))

      if choice == '2':

        #Generate a Calendar Month from user input

        year = input("\n Year (Ex: 2013): ")
        while True:
              try:
                  month = int(input(' Month (Ex: 4): '))
              except ValueError: # just catch the exceptions you know!
                  print('That\'s not a number!')
              else:
                  if 1 <= month <= 12: 
                      break
                  else:
                      print('Out of range. Try again')

        print("\n Here it is: \n")
        print((calendar.month(int(year), int(month))))

      if choice == '3':

            from datetime import date

            year = int(input('\n Year (Ex: 2013): '))
            month = int(input(' Month (Ex: 4): '))
            day = int(input(' Day (Ex: 15): '))

            dow = date(year, month, day)

            print('\n %s = %s' % (dow.strftime('%d %b, %Y'), dow.strftime('%A. \n')))

      if choice == '4':

            import datetime

            print("\nExample: \n")
            print("Enter First date: YYYY,MM,DD,HH (0-23),MM,SS, separated by commas: 2011,4,15,0,15,25")
            print("Enter Second date: YYYY,MM,DD,HH (0-23),MM,SS, separated by commas: 2013,5,26,7,32,25")
            print("772 days, 7:17:00\n")

            y1,mo1,d1,h1,mn1,s1 = eval(input("Enter First date: YYYY,MM,DD,HH (0-23),MM,SS, separated by commas: "))
            y2,mo2,d2,h2,mn2,s2 = eval(input("Enter Second date: YYYY,MM,DD,HH (0-23),MM,SS, separated by commas: "))

            d1 = datetime.datetime(y1,mo1,d1,h1,mn1,s1)
            d2 = datetime.datetime(y2,mo2,d2,h2,mn2,s2)

            print(d2 - d1)

      if choice == '5':            
            break

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download