Jump to content

Check out our Community Blogs


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

#656852 Python - Calendar - Input, Error Catching, While Loops

Posted by elcentro3m on 18 April 2013 - 12:40 AM

#Terminal Syntax: python3 cal_choose.py
#Python 3.2

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")

      if choice == '2':

        #Generate a Calendar Month from user input

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

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

      if choice == '3':

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


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.

  • 1

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