|
Date: 02/18/06 15:18:14
Subject: Re: [Tutor] Need to write a python and call it within a python main program (fwd)
Just as a note: please use your email client's "Reply to All" feature so
that our correspondence stays on the Python-tutor mailing list. The idea
is that if I say something that is inaccurate or incomplete, the other
helpers can come and compensate for my weaknesses.
*********
ok
***************
> ***************
Python has a familiar control-flow syntax for branches in the 'if'
statement. For example, here is an interactive session that shows how one
can use it for a simple case-analysis:
######
>>> def is_even(n):
.... if n % 2 == 0: ## % is the modulo operator
.... return "yes"
.... else:
.... return "no"
....
>>> is_even(42)
'yes'
>>> is_even(13)
'no'
######
******
This is a very good example.
Using it, I translate my pseudo code
set j = 8 * mod(a,2) + 4 * mod(b,2) + 2 * mod(c,2) + mod(d,2)
if j = 0, a,b,c,d = a/2,b/2,c/2,d/2 if j = 1, a,b,c,d = -1,-1,-1,-1 if j = 2, a,b,c,d = a,b/2,c,d/2 if j = 3, a,b,c,d = a,(a+b)/2,c,(d-c)/2 if j = 4, a,b,c,d = a,b,c/2,d/2 if j = 5, a,b,c,d = a,b,(a+c)/2,(d-b)/2 if j = 6, a,b,c,d = b,a,d,c if j = 7, a,b,c,d = b,a,d,c if j = 8, a,b,c,d = -1,-1,-1,-1 if j = 9, a,b,c,d = 2*a,a+b,a+c,(d-a-b-c)/2 if j = 10, a,b,c,d = b,a,d,c if j = 11, a,b,c,d = 2*a,a+b,c,(d-c)/2 if j = 12, a,b,c,d = b,a,d,c if j = 13, a,b,c,d = 2*a,b,a+c,(d-b)/2 if j = 14, a,b,c,d = b,a,d,c if j = 15, a,b,c,d = a,a+b,a+c,d-a-b-c
into
>>> def trans01(a,b,c,d): .... j = 8 * a%2 + 4 * b%2 + 2 * c%2 + d%2 .... if j = 0, a,b,c,d = a/2,b/2,c/2,d/2 .... if j = 1, a,b,c,d = -1,-1,-1,-1 .... if j = 2, a,b,c,d = a,b/2,c,d/2 .... if j = 3, a,b,c,d = a,(a+b)/2,c,(d-c)/2 .... if j = 4, a,b,c,d = a,b,c/2,d/2 .... if j = 5, a,b,c,d = a,b,(a+c)/2,(d-b)/2 .... if j = 6, a,b,c,d = b,a,d,c .... if j = 7, a,b,c,d = b,a,d,c .... if j = 8, a,b,c,d = -1,-1,-1,-1 .... if j = 9, a,b,c,d = 2*a,a+b,a+c,(d-a-b-c)/2 .... if j = 10, a,b,c,d = b,a,d,c .... if j = 11, a,b,c,d = 2*a,a+b,c,(d-c)/2 .... if j = 12, a,b,c,d = b,a,d,c .... if j = 13, a,b,c,d = 2*a,b,a+c,(d-b)/2 .... if j = 14, a,b,c,d = b,a,d,c .... if j = 15, a,b,c,d = a,a+b,a+c,d-a-b-c IndentationError: expected an indented block (<pyshell#0>, line 2) >>> >>> >>>
Why did I get this error message?
|