conditionals in fig

this is part 6 of an introductory series on fig programming. for part 5, go here.

 

part 6: conditionals in fig

 

weve covered a couple kinds of block so far:

  • python marks lines written in python, then fig ends the block
  • while marks lines that will loop, then wend marks the bottom of the lines that will repeat
  • forin and for mark the beginning of a loop through numbers or arrays, and nextin or next marks the bottom of those loops

 

now we have another kind of block:

  • a condition describes part of the state of the running program, and:
  • the lines inside the block only run if the condition is satisfied / true

 

normally the “part of the state” is just the value of a variable.

like with python these blocks will end with the fig command. here is a very simple example:

iftrue 1
    now "that is true"  print
    fig

 

an iftrue conditional has one parameter, and if the parameter is “true” (non-zero) then it will run the code inside the block (between the conditional and the fig command.)

in the previous example, 1 is non-zero so it runs the code in the block:

now "that is true"  print

 

what isnt true? a zero, or a zero-length string:

iftrue ""
    now "this will not print"  print
    fig

 

so what about iffalse? for that, fig has a command that will invert a conditional, but first lets talk about blocks without code inside:

iftrue ""
    fig

 

will give you an error message when it runs. use pass as your placeholder for actual code:

iftrue ""
    pass
    fig

 

the ifequal conditional will compare two parameters, and is true if they are equal:

x "when is your birthday?"  print lineinput
d date

ifequal x d
    pass
else
    x "a very happy un-birthday to you!"  print
    fig

 

this program (assuming you type the date in using mm/dd/yyyy format) asks you to type in your birthday, and if the date is not today’s date, it wishes you a happy un-birthday.

the else command separates a conditional block into a two-part conditional block:

  • if the first part is true, it runs the code between the condition, and else
  • if the first part is NOT true, it runs the code between else and the bottom of the conditional

 

lets use the randint function with its “lowest” and “highest” parameters to product a number between 1 and 10. then ask the user to guess the number:

x randint 1 10
y  "guess a number from 1 to 10: "  print  lineinput

ifequal x y
    now "you guessed correctly!"  print
else
    now "good try, but you didnt guess it."  print
    fig

 

something is wrong with this program, and it will never work the way its intended to. but we can fix it!

the randint command returns an integer, but lineinput returns a string. when ifequal compares an integer and a string, it will NOT detect a match even when one is string “5” and the other is numeric 5!

we can fix this by making the value from randint into a string using str, or making the output from lineinput into a numeric using val. since we dont know what to expect from lineinput (the user could type anything) we will convert the randint:

x randint 1 10  str
y  "guess a number from 1 to 10: "  print  lineinput
ifequal x y
    now "you guessed correctly!"  print
else
    now "good try, but you didnt guess it."  print
    fig

 

by adding str to the end of the randint output, we ensure that when ifequal compares it to the number the user types in, at least a string is being compared to a string.

there are other improvements worth making, but for now this is good enough. (which commmands will compensate for extra spaces on either side of what the user types in?)

this is a great time to add a while loop. currently the user has one guess, and we could give an exact number of guesses with a for loop, but that wouldnt demonstrate a while loop doing something truly functional, would it? lets give unlimited guesses, so you can see how to do that:

while
    x randint 1 10  str
    y  "guess from 1 to 10: "  print  lineinput

    ifequal x y
        now "you guessed correctly!"  print
        break
    else
        now "good try, though."  print
        fig
    wend

 

4 things to notice here: the while at the top– the wend at the bottom– plus, a correct guess stops the loop using break after telling you that you guessed correctly.

but perhaps the most important thing is the line with randint on it. because once again, it will not work (exactly) the way that we intend it to:

while
    x randint 1 10  str
    y  "guess from 1 to 10: "  print  lineinput

 

do you see whats happening? it gets a random number and gets the user to input a guess. ok… when it loops, it gets another random number!

since the number is always 1-10, the user can just keep guessing 5 (or any other number in range) until the computer picks the users number randomly! so lets have it pick the number ONCE:

    x randint 1 10  str
while
    y  "guess from 1 to 10: "  print  lineinput

    ifequal x y
        now "you guessed correctly!"  print
        break
    else
        now "good try, though."  print
        fig
    wend

 

now it works the way it is designed to: the user has to keep guessing a different number until they can guess the one the computer picked.

but now that youve seen the while loop work in a way that the program can get out with a conditional break, lets replace it with a for loop so we can limit the number of guesses the user gets:

guesses 4
x randint 1 10  str
for r  1 guesses 1
    y  "guess from 1 to 10: "  print  lineinput

    ifequal x y
        now "you guessed correctly!"  print
        break
    else
        now "good try, though."  print
        fig
    next

 

we didnt have to change wend to next, it just looks more reasonable that way. also we didnt have to make a variable called guesses, we couldve just put a 4 between the 1 and 1. but this is clearer.

lets take a look at the output this program will show the user, along with the numbers the user might type in:

guess from 1 to 10:
5
good try, though.
guess from 1 to 10:
2
good try, though.
guess from 1 to 10:
8
you guessed correctly!

 

how about another condition to say the user ran out of guesses? the for loop keeps track of which guess the user is on, in the r variable, right? we can use that:

guesses 4
    x randint 1 10  str
for r  1 guesses 1
    gsss guesses  minus r  plus 1  str
    y "you have " plus gsss  plus " guesses."  print
    y  "guess from 1 to 10: "  print  lineinput

    ifequal x y
        now "you guessed correctly!"  print
        break
    else
        now "good try, though."  print
        ifequal guesses r
            now "sorry, no more guesses."  print
            fig
        fig
    next

 

here are the relevant lines of the changes we made:

guesses 4
    x randint 1 10  str
for r  1 guesses 1
    gsss guesses  minus r  plus 1  str
    y "you have " plus gsss  plus " guesses."  print

 

also:

        now "good try, though."  print
        ifequal guesses r
            now "sorry, no more guesses."  print
            fig

 

these two changes dont need each other; the second change would still work by itself, and the stuff at the top could work without the bottom part.

lets go over what we know:

  • the for loop will repeat guesses times, which in this case is 4.
  • the loop tracks which repeat (which guess) its on, in the variable r.
  • so if r is the same number as guesses, it is on the last guess (in this case, it was already used.)

 

that pretty much explains the part at the bottom. it doesnt run until after the program tells the user they guessed wrong, so if guesses is the same number as r, the last guess was used.

what about the top part? this is just addition and subtraction:

    gsss guesses  minus r  plus 1  str
    y "you have " plus gsss  plus " guesses."  print

 

  • gsss is set to the limited number of guesses (4.)
  • then r (which guess user is on: 1 or 2 or 3 or 4) is subtracted, so if guesses is 4 and r is 1 (first guess) then there are 3 guesses left.
  • but… the user hasnt guessed yet! so we add 1 more:  “plus 1” since guess 1 is still in play.
  • then we convert that number to str and put it in a string between “you have ” and ” guesses.”
  • then we print it.

 

from the top, it looks really complicated. but if you want this functionality, its just:

gsss guesses  minus r  plus 1  str
y "you have " plus gsss  plus " guesses."  print

 

you may find with logic like this, that the
stricter syntax in python is easier to work with.

here is what that looks like:

guesses = 4
from random import randint
x = str( randint(1, 10) )

for r in range(1, guesses + 1, 1):
    gsss = str(guesses – r + 1)
    y = "you have " + gsss + " guesses." ; print y
    y = "guess from 1 to 10: " ; print y
    y = raw_input()

    if x == y:
        print "you guessed correctly!"
        break
    else:
        print "good try, though."
        if guesses == r:
            print "sorry, no more guesses."

 

some of the differences between fig and python:

  • python cares what case you use (normally all-lower. but the same variable name, capitalized differently, counts as two different variables.)
  • parentheses are not usually optional (in fig they are.)
  • you must use = to set a variable, and == to compare (fig requires neither; = can set variables.)
  • functions(are(parenthetical + str(that) + can())) #### become complicated.
  • instead of starting blocks with one command marking the bottom with fig or another command, you have to indent (in fig, this is optional) everything after a block starter. when you are done with the block, you un-indent.
  • colons at the end of a block-starter are generally required, in python.
  • python 3 and python 2 differ from each other in how they use the print command.
  • many commands, like randint, have to be imported.

 

believe it or not, this isnt meant to discourage you from using python. if youre comfortable with mandatory indentation, it can be very beautiful (i wrote fig in python, so i know its a great language.)

but when i try to teach python to people, i noted what gripes came up the most. mandatory indentation is good for some and terrible to others. fig doesnt have it. case-sensitivity is fine for most coders– i preferred basic when it was not case-sensitive (in some dialects it is now.)

if you love python, here is an approximation of the same code in fig, using optional () and = and : and such:

# a program in pure fig code
guesses = 4
x = randint(1, 10) ; str()

for r (1, guesses, 1):
    gsss = guesses ; minus r ; plus 1 ; str()
    y= "You have ";plus gsss;plus " guesses.";print()
    y = "Guess from 1 to 10: " ; print()
    y = lineinput()

    ifequal x, y:
        now = "You guessed correctly!" ; print()
        break
    else:
        now = "Good try, though." ; print()
        ifequal guesses, r:
            now = "Sorry, no more guesses." ; print
            fig
        fig
    fig

 

obviously you cant dress up fig exactly like python (without putting it between python and fig commands, that is) but you can dress up fig, if you want to.

whatever you consider “simple” is what you should stick to code-wise, until you are ready and interested in getting more complicated. when youre ready, there are lots of things to try, including making new fig commands using function and/or inline python. and there are more conditionals!

to review: else inverts a conditional, and pass is a placeholder (in case you dont want to run code for every condition, but you do want to run code for some.)

using a comment # instead of pass will not work in loop and conditional blocks. python is the same way– the pass command does exactly the same thing in python code.

ifequal  x y
    now print "yes"
    fig
ifmore  x y
    now print "yes"
    fig
ifless  x y
    now print "yes"
    fig

 

the ifmore (x, y) conditional is true only if x is more than y.

the ifless (x, y) conditional is true only if x is less than y.

substitute x with some other variable or value; and/or substitute y with some other variable or value.

once you feel confident that you understand conditionals, else and pass, there is a very useful conditional-like block that is very special: the “condition” it handles is an error.

it works like this:

try
    pass
    # try to do this
except
    pass
    # run this code if there was an error before
    resume

 

resume is another substitute / alias for fig which you can use instead (as usual) if you prefer.

try
    x 5  divby 0
except
    x "you cant divide numbers by zero."  print
resume

 

catches a real error. of course no one thats paying attention is going to write “divby 0” in their program, but with divby y the y could be anything; so this example is more useful in a real program than it may first appear.

you dont need to use error catching on every use of the divby command, but its quite often worth it in situations where user input (or even input from a file) is involved.

you can use a try / except / resume to try opening a file that isnt available (or that the user didnt spell the name of correctly,) or you could put it in a loop that tries to convert user input to a numeric, and asks again if it cant:

while
    x "please enter a number: "  prints  lineinput
    try
        y x  val
        # if that didnt trip except, break loop
        break
    except
        now ""  print  "thats not a number."  print
        resume
    wend
now y  str  plus " times 5 is: "  prints
now y  times 5  print

 

that functionality we just created could be used more than once in a program that asks for numbers. lets simplify it before getting into functions:

while
    x "please enter a number: "  prints  lineinput
    try
        y x  val
        break
    except
        now ""  print  "thats not a number."  print
        resume
    wend


now y print # this is the number we know is numeric.

 

 

loops in fig

this is part 5 of an introductory series on fig programming. for part 4, go here. for the next part, go here.

 

part 5: loops in fig

 

this is really the first chapter about “program blocks.”

so far, weve covered one type of block– a snippet of inline python:

python
    y = (5 + 12) * 3  # 51; different than:
    y = 5  + 12  * 3  # 41; (order of ops)
fig

 

this block begins with the python command, and the end is marked with the fig command.

the defining characteristic of a block is probably that it starts and ends with a pair of commands.

in fig, you can end any block with the fig command. but semantically it makes the most sense when it ends a python block, because python means: “here is some python code” and fig means: “get back to the fig code.”

all loops are program blocks, also known as “command blocks.”

between the start and end of the loop blocks are the lines of code that will “loop.”

fig has three kinds of loop: for, while and forin.

the simplest loop is the while loop. lets make a single line of code to print a zero:

x  print

 

now lets start a while block by putting it on the line before the one we just wrote:

while
x  print

 

we need to mark the end of a loop, or fig will loop every line that comes after the while command:

while
x  print
fig

 

there is nothing wrong with this block; its perfectly formed. however, while blocks have their own end marker, which you can optionally use in place of the standard fig command. it does exactly the same thing, but helps note which block it ends:

while
x  print
wend

 

so you can end a while block using the fig command, or you can end it using wend (it stands for while-end.) the choice is up to you, they are interchangeable.

the while command exists in basic, python and even c. in basic and python, you can use while true: or while 1: to keep looping until the loop breaks with break or (in basic) exit while.

since for more than half a decade, ive used while 1 in python instead of worrying about setting up the loop with a condition, fig is designed this way for simplicity.

while
x  print
wend

 

prints 0 (or an array stored in x, where applicable) repeatedly until the user breaks with ctrl-c on the keyboard.

besides ctrl-c, the other way to break out of a while loop is using the break command (same as in python,) but without a conditional (covered in the next chapter) a break command will either prevent the loop itself, or prevent the lines inside it from running:

while
x  "this only runs once-- no loop"  print
break
wend

 

the break command stops the loop right after it runs the code inside it once, so it is as if the while command isnt there at all; only the line that prints.

while
break
x  "this line doesnt run at all"  print
wend

 

moving the break command to before the other line inside the loop exits before the lines after it can even run– so this entire block does nothing other than waste a tiny amount of time.

in order to make a while loop do something useful, you probably want to put the break command inside a conditional block. we will get to those in the next chapter.

a forin loop will loop through the items in an array, running the same code inside the block for each one. these items can be lines of a file, lines in a website, letters of a string, pieces of a string made into an array by split– any array.

lets do words in a string, by splitting the string into an array by spaces:

names "ady susan kerry morgan lori"  split names " "
forin p names
    now p  print
    fig

 

fig will split the string by ” ” to make an array called names, and print each one:

ady
susan
kerry
morgan
lori

 

we end the forin loop using fig again, but just as while has wend for (optional) semantic use, forin has nextin (or next). use whichever you prefer.

forin p names
    now p  print
    nextin

 

if you want to loop through numbers, you can use a for loop. a forin loop will run the same code repeatedly, once per array item– and a for loop once per number in a range– for has 4 parameters:

  • the variable for will set with the value of the current item (just like a forin loop)
  • the number to start with
  • the number to stop at
  • the “step”; the number to increase on each loop

 

in other words:

for v start stop step

 

suppose we want to bring variable size from 5 to 12, doing every whole number in that range:

for size  5 12 1
    now size  prints " " prints
    next

 

will output: 5 6 7 8 9 10 11 12

odd numbers from 33 to 17?

for s 33 17  -2
    now s  prints " " prints
    next

 

33 31 29 27 25 23 21 19 17

one quirk of python is that it insists on integer (whole number) steps in numeric ranges. in python (which is what fig translates its code to,) a for (range) loop isnt going to do a decimal step.

for v start stop step # step has to be integer
    now v  print
    next

 

however, if you use a constant for the step instead of a variable, fig will let you do a float step:

start 5
stop 10
for v start stop 2.5 # step is constant 2.5
    now v  prints " " prints
    next

 

outputs: 5.0 7.5 10.0

this builds a different kind of loop (actually a custom while loop) in the python translation, instead of a genuine for loop.

in the next chapter, while loops will become more useful when they can start and stop based on conditions. certainly you can use them anyway, in any situation where using ctrl-c on the keyboard to stop looping is suitable.

 

 

basic math in fig

this is part 4 of an introductory series on fig programming. for part 3, go here. for the next part, go here.

 

part 4: basic math in fig

 

y 5  plus 12  times 3

 

adds 12 to 5, then multiplies times 3.

fig always goes left to right. if you want to do parentheses and order of operations, use python:

y 0  # most important part right here
python
y = (5 + 12) * 3 # 51; different than:
y = 5  + 12  * 3 # 41; (order of ops)
fig

 

set y in python, to 5 + 12… then * 3

then reset y as 5… + (12 * 3) or: 5 + 36

inline python, unlike the rest of the fig language, requires specific indentation. lines begin at 4 spaces from the left, and each indent increases by 4 spaces.

python is not really part of the fig language; but fig allows you to include snippets of python in your fig programs. since fig translates to python, those sections will be included un-translated.

notice that before y is used in the python code, fig sets y to 0 before switching to inline python.

y 0

 

ensures that fig knows y is a valid variable in use, so that after python uses it fig already has it registered. after the inline python runs, fig will use the value python left it with.

you can avoid this extra step if you know that y is already:

  • used as a main variable in fig at least once
  • has a value that the python code can use or reset

 

y 0
python
y = 5
fig
x y  minus 2.5  print

 

sets y to 0, uses python to change it 5, sets x to y  minus 2.5, (which is 2.5) and prints x.

plus and times also work on strings and arrays.

x  minus 5

 

sets x to -5. does this by setting x to 0 and subtracting 5 from x.

y 200
x y  divby .5

 

sets y to 200, copies y to x and divides by .5

x 25  oct print

 

why do programmers mix up halloween and christmas?

because oct 31 is dec 25.

decimal        octal        hex
0     16       0   20       0 10
1     17       1   21       1 11
2     18       2   22       2 12
3     19       3   23       3 13
4     20       4   24       4 14
5     21       5   25       5 15
6     22       6   26       6 16
7     23       7   27       7 17
8     24       10  30       8 18
9     25 <--   11  31 <--   9 19
10    26       12  32       a 1a
11    27       13  33       b 1b
12    28       14  34       c 1c
13    29       15  35       d 1d
14    30       16  36       e 1e
15    31       17  37       f 1f
x 255  hex

 

sets x to “0xff”

x 50
y 70
r 40
x2 3.14  cos  times r  plus x  int
y2 3.14  sin  times r  plus y  int

 

sets x2 to the cosine of 3.14 radians, multiplies
that by r, and adds x then converts to an integer.

then sets y2 to the sine of 3.14 radians, multiplies that by r, and adds y then converts to an integer.

if instead of 3.14, you use another variable that starts at -3.14 and increases in value until it is equal to 3.14, this will plot a circle on points (x2, y2).

x 1  atn  times 4

 

sets x to the arctangent of 1 and multiplies that by 4, which gives pi to at least 11 decimal places.

x 3.14  tan

 

sets x to the tangent of 3.14 radians.

x 2.5  int

 

sets x to 2.5 and converts to an integer.

x -5  sgn

 

changes x to either -1 (if the value is below 0) or 1 if the value is above 0. if the value is 0, it stays the same.

in this case, x becomes -1.

x 25  sqr

 

sets x to 25, and then to the square root (5).

x 255  mod 7

 

sets x to 255, then to 255 modulus 7.

x 1024  topwr 2

 

sets x to 1024, then 1024 to the power of 2.

 

 

learning how to code is not always easy

“I wrote this letter to myself as a part of an assignment in the Udacity Front-End Web Developer Nanodegree course I’m previewing today…”

Source: Learning how to code is NOT easy

 

cool…”front-end web developer nanodegree.” i have no idea what that is. (ok, i can sort of guess.)

i used to make websites, they were easy. now every site has to have a database (not true, but theyre nice) and they need a javascript library/content engine (not true, but they definitely do wonders for big websites) and you have to use html5, css and a python, php or node.js backend (all optional.)

they can make all of this complex, and industry always will. i think its unfortunate, because the bells and whistles one person will say are necessary is just a bloated pain to someone else. if you teach computer science, you might still have a webpage done in html 3 without css.

but the worst part is, modern education insists that people start in the middle. its not completely crazy; if you can start in the middle, there are certainly rewards. you get to see the “big picture” before you even understand the details. you get started right away on results, before you even know how to achieve them. i mean thats how it is with software these days– people use it, but they still dont know how.

all the same, thats not the only way to learn about computers, and its not how i learned, either. when i started with computers, i only used two programs– a paint program (great for building skills with the mouse, also good for art) and basic. so you would get a little spiral-bound book out:

3baa6eaa515090f55bbee15a97482e4c3

 

and simply follow the instructions. type this:

10 PRINT "hello world!"
20 COLOR 5
30 GOTO 10

 

then you would type “run” and hit enter. it would say hello, world! on the screen, then change the color to purple and say it again, and again, and again, until you hit ctrl-c.

if youre 5 years old and can read, you can do this. it starts out slow, but in a way that can grab your interest for the next 20 years.

ive had lots of hobbies. this one stuck– i kept teaching myself from that point onward until i wrote my own programming language. its not the best or the fastest programming language in the world (thats partly because i chose to implement it in python, and partly because speed or “being best” or fast, wasnt a design goal) but its designed to bring this sort of learning back for everyone.

if youre doing web design, youre already learning 2 or more languages at once. when it was just html, coding for the web was easy! just put little things in less-than/more-than signs < > and add the things you want to say, or links to pictures. a 5-year-old could do it.

more importantly, to someone that doesnt have a long background in coding, they may not realize that all code bears incredible similarities to all other code. on the surface, they can be very different. but the surface is abstract; its different because it was made differently, not because it has to be (or even really is, by the time it gets to the computer.)

im not attacking abstraction– my entire programming language is an abstraction over python, which is an abstraction over c, which is an abstraction over the code that the computer likes. but when you find that “coding is hard,” its hard mostly due to the abstraction making things complicated.

some of these complications make plenty of sense– others are only well-intended. some are even complicated for deliberate and dubious (market-grabbing) reasons.

if you want to become a professional coder, you can start with the easy stuff or you can start in the middle. but the “middle” these days is pretty complex stuff. im not sure why they dont take a little more time with the beginnings, because its not just that the basics are so much easier to learn (and teach.) they also give you a fundamental understanding of all the other stuff youre going to learn in the “middle.”

because all that complex stuff is based directly on the basics. thats a lesson that most of these kids arent getting, which is too bad. theyre learning for an industry first, and education second. theres really nothing wrong with doing it the other way around. it might take a little more time, and it can reach a lot more people without the feeling of “i cant do this, i cant do this.”

even if people knew there was more than one way to learn this stuff– that would help a lot.

why should it matter if coding is accessible to kids or not? #1 reason: by age 10 (on average,) they already identify as “computer people” or “non-computer people.” so many people fall into the latter category, and have a solid belief that they either cant learn, or that it will be painfully difficult. learning can be made difficult, but one of the things that contributes heavily to that is the belief that it will be– if you think learning is going to hurt, its going to be hard to get your attention long enough to prove you wrong.

i wrote fig specifically to get at the people who insist it is difficult. ive shown a handful already that programming can be fun and easy (without being cartoon-based, but based on typing actual code) but while im trying to become a better teacher, im also trying to learn how to “sell” the idea. the program is free, but time is valuable– so you still have to sell to people for the time they will spend.

to every person struggling– dont give up! but you do always have the option of trying more than one thing. coding can be easier, and you can help make it easier. either way someone has to share their passion for it, and that passion has to catch on. a lot of teaching is shared learning, and inspiration– good luck with it.

 

 

find and zip all .py files with bash

Create a Zip folder including all .py files on your computer.

via Find and Zip All .py Files — Data Science for Engineers

 

not to be obtuse, its a good demo of pythons capabilities; but it didnt take very long to figure out this one-liner to do more or less the same:

echo found $(find . -name '*.py' -print0 | 
xargs -0 zip all-python-source-files.zip -@ | wc -l) 'file(s)'

 

 

98-100 followers, and a shared comment on teaching

ive been online since the mid 90s, when the internet was infused with the portion of it called the “web.” before the web and since november 1969, the internet has included technologies like email, ftp, and eventually usenet and gopher (the text-only precursor of the once-text-only hypertext worldwideweb.) and i have met some of the loveliest people online on wordpress– i am not exaggerating– it is one of the better examples of what the web can achieve without facebook. 🙂 and what i was trying to say of course is, thank you!

i wrote a comment to a fellow poster today, in which we talked about quitting smoking, drinking less soda, and eventually having kids. if i write a comment i like that much, i may turn it into a blog post, so here it is:

 

youre going to have awesome kids. i can only offer one bit of advice as a former kid myself–

kids are definitely made in the image of their parents. its always there, people can see it, youll know it better than everyone. and parents raise their kids in the image of themselves, too. its what they know, so its what they do.

kids are trouble, and theyre people that are learning how to be people. learning is a lifelong process, which is frequently forgotten in the moment by students and teachers (all parents are teachers, as well as students) alike.

teachers too, shape minds in their own image. its what they know, and what they do. the advice is: never forget that the image will differ. cherish that– their difference will add new powers and also balance to the things in your image that you instill in them. they will never/can never/should never be exactly you, but they will carry part of you around forever– it will go much farther with their differences, than you could ever go on your own two feet or even just being cloned. remember that as you work to shape them. ❤

 

(regarding 98-100: after almost a year of blogging its fun to reach 100 followers, but i think 2 of those 100 are probably me. i set up another page which im not making much use of right now, and i had it follow this one for convenience.)

 

 

output commands in fig

this is part 3 of an introductory series on fig programming. for part 2, go here. for the next part, go here.

 

part 3: output commands in fig

 

while input commands get information from a device (or file, or connection, or from some part of the computer,) output commands send information to a device, or file, connection, or to some part of the computer.

obvious output devices are the screen and speakers. sending files to the printer counts as output, although the printer itself also sends information back to the computer, making it an input/output device.

a touchscreen is a separate device than the screen itself and an input device, although the display directly behind it is for output. these are technical distinctions and not always important, but are sometimes relevant to programming.

x  "hello there"  print

 

sets x to a string and sends x to stdout, which generally means the screen (or window.)

x  "hello there"  prints

 

like the previous line using print, except prints stays on the same line instead of advancing.

x   "text.txt"  open "w"
y   "ok"        fprint x
now x           close

 

you would normally find these used separately.

the first line sets x to a string containing the path and filename of the file to save information to; then it opens the file for writing (the “w”.)

the fprint command writes the value of the main variable to x, which is still holding the filename. in this case, the string “ok” is saved.

the close command will update / save / stop writing to a file that was opened before. it closes whatever file is specified by the main variable. dont close the file until youre done fprinting (each and every line you want to save) to it.

each time you open a file for writing, it starts writing at the beginning. python lets you “append” or add to a file, which could even be added as a feature to fig.

(it doesnt have that feature yet. if it did, you would open “a” for append, instead of “r” for read or “w” for write.)

if you need to, you can already add to a file this way:

  • open to an array using arropen
  • open again using open “w”
  • write the array with the existing information using fprint, before adding to it.

 

writing an array using fprint is possible by using a loop, or by turning the array into a string, using join. both loops and the join command are covered in later chapters.

most languages use a filenumber or handle to track an open file; which is a different thing than a string or a numeric variable. ive always thought that is weird or unfriendly for beginners.

for python coders and the extra curious: fig keeps the handles in a special type of array called a dictionary, with strings as the key to each handle. fig coders dont need to worry about any of that, so long as they make certain to use the same string (contents) to close or fprint that they used to open it.

in other words, if you use the string “../text.htm” with the open command, you will need exactly the string “../text.htm” to read or write or close that
file. it doesnt matter if it’s a constant (written out) or two variables holding the same value, as long as theres a perfect match.

for what its worth, ending the program with end or system (or just letting the program stop normally) is supposed to close all the files that are still open.

x  cls

 

clears (and goes to the top left of) the screen.

x  display

 

the first time this is called, it turns off graphics auto-updating (the default is to update every time you use a graphics command.)

if already called once, display updates the screen with any graphics that were drawn to it. this only affects the graphics windows, not “text-mode” graphics.

textmode

 

forces text-mode graphics– or dont try to display graphics commands in a graphics window; display them using ansi and unicode text codes.

graphics

 

the default mode; open a graphics window when any graphics commands are used. if the graphics window fails (perhaps pygame is not installed) then fig falls back to text-mode graphics.

if textmode was used explicitly, the graphics command turns real graphics back on.

x  line 5 7  10 20  14

 

draws a line from point (5, 7) to point (10, 20) in yellow– where (5, 7) is the 5th dot horizontally and the 7th pixel vertically.

x  pset 5 7 10

 

draws a point at (5, 7) in light green.

x  locate row column

 

move to (row, column) on text screen.

x  colortext 5

 

changes text color to magenta:

0 black         8 grey
1 blue          9 l. blue
2 green         10 l. green
3 cyan          11 l. cyan
4 red           12 l. red
5 magenta       13 pink
6 brown         14 yellow
7 white         15 b. white

 

x highlight 0

 

changes text background to black:

0 black         4 red
1 blue          5 magenta
2 green         6 brown
3 cyan          7 white