KwicKode Tutorial Part A: The Basics
Order Kwickode Today:
KwicKode – Springboard to Python
Go to Tutorial Part B: Loops and Functions
Go to Tutorial Part C: Lists and File I/O
| Caution: This tutorial presents only a cursory overview of the KwicKode commands. It in no way purports to teach programming. Designing an application and details of program structure are covered in more detail in my book, KwicKode-Springboard to Python. |
KwicKode consists of sixteen basic commands, with some minor variations to accommodate lists. With one exception, every line of code begins with a capital letter and ends with a period, just like a normal English sentence. The one exception deals with blocks, which are several lines of code that perform a specific function. Blocks are delineated with square brackets and may optionally be indented for visual clarity. (Python requires indentation for coding blocks.) The first line in a block begins with a left square bracket and ends with a colon. The last line in a block is always End of xyz block.] where xyz is either ‘function’, ‘if’, ‘else’, or ‘repeat’, identifying the type of block being ended. Note the right closing square bracket.
Here is the 4-line program presented on the previous page:Comment: Simple example of KwicKode commands.
Display "Please enter your first name...".
Get string for my_name.
Display "Glad to meet you, ", my_name, "!".
This program illustrates four of the five simplest commands, the fifth being the Assign abc to xyz. command which assigns a value (abc) to a variable (xyz), as in: Assign "Dan Bishop" to author_name. or
Assign 3.14159 to pi.
The following sample program illustrates how these five commands are used as well as a simple ‘if/then/else’ block. (Note: Indentation for blocks is optional, but makes nested blocks easier to identify. The first line in a block is not indented but all the rest are, including the ‘End of…” command. (The accent marks ““ in the listing are used to force Word Press to recognize tab indents.)
Comment: Sample KwicKode program using nested conditional if/then/else blocks.
Display “Please enter your last name…”.
Get string for user.
Assign user[0].upper( ) to last_initial.
Assign “salad”,”side-dish”,”dessert” to list dishes.
[If last_initial < “K” then:````Assign 0 to dish_index.````End of if block.]
[Else:````[If last_initial < “S” then:````````Assign 1 to dish_index.````````End of if block.]````[Else:````````Assign 2 to dish_index.````````End of else block.]````End of else block.]
Display dishes as list.
Display “You are to bring a “, dishes[dish_index],” to the potluck.”.
There are several things to note here.
Comment: …. All KwicKode programs must begin with a Comment: …. command. Other than that, comment lines may appear (always as separate lines) anywhere in the program. Don’t forget the colon and the ending period. Everything else within the comment is ignored by the computer.
Display xyz. and Display xyz as list. ‘xyz’ may be a variable name, a quoted string of text, a Python function, or a number, and may actually be several of these items separated by commas. It simply displays the values of the items on the display monitor. Don’t forget the ending period. If the item being displayed is a list containing several items, adding the ‘as list‘ extension to the command will cause the items to be displayed in a vertical column.
Get string for xyz. This command pauses computer operation and waits for something to be typed into the keyboard followed by <Enter> or <Return>. Whatever string of characters is typed in is assigned to the variable named ‘xyz’. That variable will always be a text string (thus the word ‘string’ in the command to remind the programmer.) If the value assigned to ‘xyz’ is to be used as a number in calculations or comparisons, the Get string… input command must be followed by a command that converts the ‘xyz’ to a number. In such cases, you may choose to use the same variable name for the numerical representation. For example:Comment: Example program to illustrate string conversion to numbers.
Display "How many gallons of gasoline does your gas tank hold?".
Get string for gallons.
Assign float(gallons) to gallons.
Assign gallons*3.785 to liters.
Display "Your tank holds ", liters, " liters of gasoline.".
In this example, the Python ‘float(…)‘ function in the assignment statement converts a string to an decimal number. For integer conversions, use the ‘int(…)‘ function instead. Important note: Every ‘Get string…‘ command in KwicKode must be preceded by a ‘Display…‘ command that provides a prompt telling the user what is expected for input.
Assign abc to xyz. and Assign abc, lmn to list xyz. The value provided by ‘abc’ is assigned to the variable ‘xyz’. This means that whenever ‘xyz’ appears in the program, the computer will use its current value in calculations and comparisons and functions. ‘abc’ may be a quoted string of text, a number, a function or math expression, situations illustrated in the previous code segment. A comma separated list of values may also be assigned to a list variable, in which case the word ‘list‘ is added to the command, as in the ‘side_dish’ example above. There is no way to distinguish by the name alone whether a variable represents a single value or a list of values. (Unless, of course, you name it accordingly, as in ‘dishes_list’ instead of just ‘dishes’.
A note about lists. A list is merely a group of items represented by a single variable name (side_dishes in the above example.) Each item carries with it an index number, beginning at zero. To call out the value of a single item from the list, attach the index number for that item in square brackets to the list-variable name. (Example from above: ‘dishes[dish_index]’ where the variable ‘dish_index’ was assigned a value of 0, 1, or 2.) Incidentally, KwicKode, like Python, treats strings as a special type of list. So, the first letter appearing in a string has an index value of zero. Thus, in the sample program, ‘user[0]’ represents the first initial of the user’s last name. Since the conditional operations that follow require that character to be upper case, an assign command is used with the Python method ‘.upper( )‘ to convert lower case to upper case characters. This would allow for someone failing to capitalize their last name.
[If … then: A conditional block begins with a square bracket, the word ‘If’, and a conditional expression that may use any of the following conditional operators: >, <, ==, !=, >=, <=, ‘in’, ‘not in’ . Here, the ‘==’ mirrors Python’s use for “equals”, while ‘!=’ is for ‘not equals’. The ‘in’ and ‘not in’ are search operators that function on lists. For example, with the ‘dishes’ list from above, you might have an expression like:
[If ‘appetizer’ in dishes then: .
The lines of code that are governed by this [If … then: command are contained within the square brackets that end with the End of if block.] command that closes out that particular block.
[Else: Else commands must always be preceded by an [If … then: block and the commands in the [Else: block are executed only when the condition specified for the [If … then: block is false. The [Else: block always ends with an End of else block.] command that closes the block.
This chapter has covered eight of the sixteen commands that define KwicKode. In it, you have learned about variables and lists, using Python functions in display and assignment statements, and the structural elements and syntax for forming coding blocks, with the conditional if/then/else commands for an example. Part B goes into blocks in greater detail, presenting the remaining two block commands, one to control loops and the other to set up user-defined functions. Part C covers lists in more detail with three commands devoted exclusively to lists, and introduces the two KwicKode commands to handle file input and output, essential for saving and retrieving data to your computer’s data storage device.
Order Kwickode Today:
KwicKode – Springboard to Python
Go to Tutorial Part B: Loops and Functions
Go to Tutorial Part C: Lists and File I/O
Go to Home Page