Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Python - Calendar - Input, Error Catching, While Loops

calendar input error catching while loops example

  • Please log in to reply
2 replies to this topic

#1 elcentro3m

elcentro3m

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 18 April 2013 - 12:40 AM

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

import calendar

while True:

      choice = input("Please enter '1 (Year), 2 (Month), or 3 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':
            break

I got a good amount of this from the EXCELLENT site:

http://stackoverflow.com/
 

I've tried to get into the habit of including in the comments the version of Python a program works and was developed under, in this case, Python 3.2.

That way, it's easier to track it back to the appropriate version of Python and to avoid the
frustration of running a program under the wrong version of Python.

I also include the filename in the comments.  If I modify it, I change the filename and save it under a new name so as not to overwrite existing files as I fix bugs, make changes.
 

In Terminal it only worked using, explicitly, python3.  Using just python, the choices don't appear to be working.

 

I've found so much help with Python online, just thought I'd give some back for those like me, just learning the language.

 

For what it's worth.


Edited by elcentro3m, 18 April 2013 - 12:55 AM.

  • 1

#2 BenW

BenW

    CC Devotee

  • Community Manager
  • 976 posts
  • Programming Language:ActionScript
  • Learning:ActionScript

Posted 18 April 2013 - 06:47 AM

I've found so much help with Python online, just thought I'd give some back for those like me, just learning the language.

That's a great attitude to take! thanks for sharing! :)

I've moved this to the code snippets board where it's better suited :)
  • 0

fss_overall.png

 


#3 elcentro3m

elcentro3m

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 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

  • 0





Also tagged with one or more of these keywords: calendar, input, error catching, while loops, example

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