Jump to content


Check out our Community Blogs

elcentro3m

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

Topics I've Started

Python - Calendar - Input, Error Catching, While Loops

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.


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