![]() |
![]() |
A short introduction to PyNassi |
Contents:
Program structure chart "Hello World"
Strukrogramm "BMI computer"
Program structure chart "faculty"
The reader of the following chapters should know already simple bases of programming with Python, this is only a short introduction to handling of PyNassi, not a programming course.
A program is prepared in PyNassi as set of functions or classes. Each
function consists of one or more statements. A statement is an individual
instruction, a definition by cases, a loop etc..
Sample applications
In order to show the handling of PyNassi, now some examples: The step by step production of some structure charts. The finished provided program structure charts are attached to PyNassi already, you should for exercise nevertheless try to edit this program structure charts yourself.
Program structure chart "Hello"
This simple program structure chart is to show the text "hello world".
First a new program structure chart must be provided:
- activate file >New program structure chart editor, or click on the
program structure chart symbol.
- answer the question for the name of the program with "hello".
Now a program structure chart is provided consisting of
the function "hello", as well as a call of this function.
The function hello is first an empty statement. By one click with the left
mouse button on (empties) the statement instruct we PyNassi to provide the
statement
as expenditure:
The instruction is first without contents, and/or the Python instruction
"pass". With a further left-click you can assign the the text: replace
the text "text" by "Hello World". Texts (character strings and/or. Stringers)
will be always set thereby in quotation marks. Variables, values or formulas
are noted without quotation marks (see
expenditure ). The changes are confirmed with OK.
The first program structure chart is finished. You should save the program structure chart now (file > save as, or by clicking on the diskette symbol).
Subsequently, from the program structure chart the program code is produced:
file >generate Code.
PyNassi opens a new window with the Python code belonging to the structure
chart:
def hello():In order to run the code now, use "debugger > run code with Python" or the click green starting symbol. A new Python interpreter is started, and the program will run.
print "hello world"
hello()
Program structure chart "bmi computer"
We want to create a structure chart to compute the BMI (Body measure index)
of the user . The BMI is a measure for the weight in relation to the body
size. It is computed as follows:
bmi = weight * 10000/size 2
Weight in kg and body size in cm.
BMI index |
Women |
Men |
Underweight |
< 19 |
< 20 |
Standard weight |
19 - 24 |
20 - 25 |
Predominance |
25 - 30 |
26 - 30 |
Predominance in need of treatment |
> 30 |
Proceed:
First we provide a new program structure chart with the name BMI computer.
(file - > new program structure chart).
Since our program structure chart consists now of several single transfers,
we provide first a "block" (left-click into the empty statement). A block
can take up several instructions, which are worked on in sequence later.
First those must be read in indicating the user (sex, weight and size). For this we provide 3 instructions for input and store the inquiries in the three variables sex, size and weight.
Note: Sex is a character string ,
a weight and a size is numbers . The input type must be stopped therefore
accordingly.
Note: The block contains at the beginning
of only 2 empty instructions. In order to insert further instructions,
right-click the last statement, and select insert statement
below . Alternatively with the left mouse button on the yellow frameworks
of the block click. With the right mouse button instructions can be deleted
also again.
Now
an instruction is provided, and entered the formula bmi = gewicht*10000/(size
* size ). As a check now several output instructions
follow, in order to see the inputs and the computed result:
Note: Do not consider with the expenditure
to set texts in quotation marks variables, to separate and the individual
parts by commas.
Now the evaluation of the BMI result is to take place. For this first
a definition by cases takes place (men or women).
Note: The variable "sex" is a character
string, therefore here also the comparison with a character string takes place:
sex == "w" . The symbol for comparison on equality is a double
equal alike "==".
Now bypasses (definitions by cases) take place in accordance with the BMI table for the cases < 19, < 25 and remainder with "woman flax", and/or < 20, < 26 and remainder with "men". The result "underweight", "standard weight" or "predominance" is spent as output instruction
Spent end the conclusion still, both with "m" as well as with "w", a "health
warning", if the BMI value lies over 30, here is sufficient a bypass independently
of the sex.
To the overview again the entire program structure chart:
Program structure chart "faculty"
We want to provide a program structure chart, which computes recursivly
the faculty of a whole number, e.g. 5.
The faculty "fak" a number is recursively defined as follows:
fak(1) = 1
fak(n), with n more largely than 1: n*fak(n 1).
We provide a new Strukrogramm, the first function call we "start".
We change the empty statement to an expenditure contents:
"the faculty of 5 is", fak(5)Now we must provide the function "fak" in accordance with the above definition, extend thus our program by the function "fak". In addition we click on the narrow free range on the left of the program structure chart. A selection field appears, where we add "function" select. We call the again provided function by clicking the green function name too "fak". As parameters we indicate the variable "n".
"fak" we change the statement of the function to a if-else bypass , as condition indicate we:
n==1The statement change we "then" to an Return instruction with contents:
return 1We change the statement "else" to an Return instruction with contents:
return n * fak(n 1)The result should look now as follows:
The associated Python code (file > generate code):
def fak(n):
if n==1:
return 1
else:
return n * fak(n 1)def start():
print "the faculty of 5 is", fak(5)start()
We can observe the expiration of the program with
the debugger.