UNDERSTANDING INPUT AND OUTPUT


                  

[h2]WHAT IS INPUT?[h2] Input refers to any task where data outside the program can get into the running program. Commands can retrieve data from the keyboard, mouse, files, the network, or other peripheral devices. Although every form of input (and every form of output) ultimately consists of numeric values, higher level languages like python, fig and javascript can sometimes make it much easier for the coder to deal with such data. a prime example of this is when opening a file is as trivial as supplying the filename and a variable to store its contents in. instead of a stream of numbers, most modern languages will simply give you the letters, numbers and other characters are that are represented by the file. *for example, if you save a file that just says "hello", and the file is named text.txt, the arropen command:* [c12]*now arropen "text.txt"*[c12] *will open it and store each line of the file in the variable now. if you then output the contents, it will show the word "hello" on the screen.* *likewise, if you use the lineinput command, and type the phrase "this is fig" into the keyboard:* [c12]*now lineinput*[c12] *the phrase "this is fig" will be stored in the variable now.* apart from introducing figs second concept of input, these commands demonstrate a convention that adds to our concept of how the language works-- in the section on variables, we learned that you could set the value of a variable by providing a number or string: [c12]*height 5 firstname "alan"*[c12] in this section, instead of setting the variable on the left to a known value, we set the variable to the value that a fig command provides-- either arropen for the contents of a file, or lineinput for a string typed on the keyboard. [c12]*# < fig code > textfromfile arropen "text.txt" textfromkeyboard lineinput*[c12] we learned about strings in the previous section, which is the type of value that lineinput gives. but arropen is named that because it creates an array of values, which it stores in the variable now (in our first example) or the variable textfromfile (in the second example.) in the example of arropen, it actually sets the variable on the left to an array of strings. each string in the array is a single line of the text file. so far, we have learned how to set a variable and how to get information from files and the keyboard. *these examples create data that the program has access to, but that isnt useful to us unless the program can output information as well.* [h2]WHAT IS OUTPUT?[h2] output commands let the program interact with the real world, and also with other programs. theyre often as simple as input commands. the most famous output command is the print command. it is called this because before every computer had a screen, computers printed output on paper, with a computerised typewriter or printer called a teletype. also the word "print" means "to write out," and given a screen, the computer will "write out" to the screen. here is a variable: [c12]height[c12] here we set the variable: [c12]height 5[c12] here we print the variable: [c12]height 5 print[c12] output is so important, that the most classic programming example for beginners is called "hello world"-- which demonstrates output. here is "hello world" in fig: [c12]now "hello world" print[c12] first we set the variable now to "hello world," then we print it. as a result, the words "hello world" show up on the screen. the quotes in our code let the computer know the data is a string, but the quotes are not part of the string and are not displayed when the string is output. the screen just says: *hello world* you can also change the colour of the output: [c12]now = "hello world" ; colortext 5 ; print[c12] we added = and ; to make the code debatably easier to read, but they are not required in fig. they show the variable now being set to "hello world" and they show that colortext is a different command, one that changes the colour of your text. in modern versions of fig, both "colortext" and "colourtext" do the same thing. fig has 16 colours, which you do not need to memorise though if you change colours often enough you probably will memorise the table. the table has two rows, and the first 8 colours (from 0 to 7) are: [c12]0 black   1 blue   2 green  3 cyan   4 red   5 magenta 6 brown   7 white [c12] *so colortext 5 means "change the colour to magenta."* the other row of colours are a brighter version of the first row: [c12]0 black   1 blue   2 green  3 cyan   4 red   5 magenta 6 brown   7 white 8 grey   9 light blue   10 light green   11 light cyan   12 light red   13 pink   14 yellow   15 bright white[c12] [h2]UNDERSTANDING WITH GAME![h2] WELCOME TO THE SAFARI...in here youll be clicking pictures of animals by pressing the button "click" . In here, we will be taking the camera as our *input* device . The monitor on the right will give you the picture of the animal as *output* . Emjoy the ride!