python as pen and paper substitute

Johan Grönqvist johan.gronqvist at gmail.com
Tue Apr 6 17:49:47 EDT 2010


Manuel Graune skrev:
> Manuel Graune <manuel.graune at koeln.de> writes:
> 
> Just as an additional example, let's assume I'd want to add the area of
> to circles.
> [...]
> which can be explained to anyone who knows
> basic math and is not at all interested in
> python.
> 

Third attempt. The markup now includes tagging of different parts of the 
code, and printing parts of the source based on a tag.

(Sorry about the mixture of python 2.X and python 3.X print statements, 
I use 2.5)

-------------------------
## Ignore
from __future__ import with_statement
import sys

def print_selected_source(tag = ""):
     is_printing = True
     with open(sys.argv[0]) as file:
         for line in file:
             if line.startswith("## Ignore"):
                 is_printing = False
             elif line.startswith("## Show") and tag in line:
                 is_printing = True
             elif is_printing:
                 print line,



from math import pi as PI

## Show Code1
d1= 3.0
A1= d1**2 * PI / 4.0

## Ignore
print_selected_source(tag = "Code1")
print ("Area of Circle 1:\t", A1)

## Show Code2
d2= 5.0
A2= d2**2 * PI / 4.0


## Ignore
print_selected_source(tag = "Code2")
print ("Area of Circle 2:\t", A2)

Sum_Of_Areas= A1 + A2
print ("Sum of areas:\t", Sum_Of_Areas)
-------------------------


/ johan




More information about the Python-list mailing list