TGJ1O

PROGRAMMING

Programming

Class 1-2: Computer languages

Computer languages are a sequence of instructions composed to perform specific tasks through a computer.  A computer programming language is known as program source coding. A computer machine without a program cannot do ANYTHING. Computer programs can range from two to millions of lines of instructions. Just as there are several languages in the human world, there are also several computer programming languages such as Python, C++, Java, C, Perl, PHP and Ruby.

Our introduction to programming will be through a game called CodeCombat

Assignment #7: CodeCombat

Class 3: Basics of a computing language

You are to "program" a challenge for the other groups in the classroom. Students are to put a paper X on the floor somewhere within a 50 step radius of the classroom.

We will develop rules for our language in class prior to developing the program. Your group will then write a sequence of lines of code that another group can follow to find your X

Class 5-8:

Assignment #8: Lego Challenges

As per the diagram below, your robot is to pick up a weight in one "wing" of the L, and bring to the other wing of the L and drop it off. The fastest time to do so is the winner.

Take pictures of your progress and submit a google doc to Classroom that includes a brief write-up outlining your successes as well as your failures.

Here is the EV3 programming installer link

Write-UP: In a presentation document of your choice (Docs/Slides etc...) discuss what went right with your build? What went wrong? How would you fix what went wrong? Refer to research you may have to do on the web in order to apply knowledge about mechanics or programming if it's relevant to your discussion about your successes and/or failures. Include pics of your construction, coding or any other relevant pictures for the challenge(s).

Class 9-15: Introduction to structured language

smallBasic

Microsoft has a freeware package for simple programming called smallBasic. You can use the online IDE here, but is somewhat limited in what you can/can't do

Sequential Programming

Literally, a set of instructions to be run in a pre-programmed sequence. There are several other types of concepts in computer science, but we'll address those shortly. In older versions of programming BASIC sequential lines of code used to be numbered and the computer would actually run according to this number. Number lines are no longer necessary, but still, code runs in order until you tell it to do otherwise - that is from top to bottom and from left to right.

Copy the following into the code window:

'Starting with a text window for the settings

TextWindow.WriteLine("Hello World")

The first line will run, but because it has a quote it is a comment (ie used to document what happens for the programmer, but because it isn't a command it won't appear in the output). The second line runs, and generates a string (words/letters) in the output text window.

TRYTHIS: What about "printing" something else?

Press the large play button (or faster yet, hit F5 if you've installed the program) and what happens? You have written and run your first bit of code! What about a more difficult bit of code. Copy the contents of example_code to the smallbasic code window (will only run if SmallBasic is installed, not on the online IDE). Run it. What do you notice?

Some important terminlogy

Syntax errors vs. Logic errors

Some important terminlogy

a = TextWindow.Read()

a = TextWindow.ReadNumber()

How to store a variable:

'This stores a variable as a string - and this line is a comment (because of the ' sign)

TextWindow.WriteLine("Please enter your name: ")

wordInput= TextWindow.Read()

TextWindow.Writeline("Pleased to meet you "+ wordInput)

'This stores a variable as an integer ( a number)

TextWindow.Writeline("Please enter a number")

numberInput = TextWindow.ReadNumber()

TextWindow.Writeline("Your number is: "+ numberInput)

You can also set display functions (or any others) by using variables. As you saw from CodeCombat a variable is simply a letter or word that is used to represent something else. In this case the word days represents all of the text afterwards.

' if we simply do days = Monday

days = Monday

TextWindow.WriteLine(days)

' it won't display the way we want since Monday is supposed to be text and thus has to have quotes around it to display as a string

days = "Monday take 2"

TextWindow.WriteLine(days)

In many languages (eg: Python) the variable skinnyMonkey is different than the variable skinnymonkey. Make note of that as it will be the bane of your existence should you often forget. For the purposes of 'best practices':

days = "Monday take 1"

Days = "Monday take 2"

TextWindow.WriteLine(days + Days)


Variables should be named to reflect what they're being used for:

 

ballRectangleVariableName = "String Variable", note each new word in the variable name is capitalized

 

or

 

ballRectangleCoordinateName = (43,80)

 

While functions (subroutines in SmallBasic) should be named like this:

function_name_of_thing_youknow()

Note the only difference between a string (word) variable input and a number variable input is that you're changing it to READNUMBER in front of the input statement and wrapping it in a couple brackets()

A reminder the difference in the following is CRUCIAL to remember since math can only be done on numbers in all other lanugages (Small Basic is somewhat forgiving of the mistake):

wordInput= TextWindow.Read()

numberInput= TextWindow.ReadNumber()

Try this

'This stores a variable as an integer - and this is a comment

TextWindow.WriteLine("Please enter a number")

numberInput= TextWindow.ReadNumber()

mathStuff = numberInput *5 + 8

TextWindow.WriteLine("Your number is "+ mathStuff )

Try the following:

'pulls a random number and stores it. By default all random numbers start at 1

randomNumber = math.GetRandomNumber(10)

TextWindow.WriteLine("Your number is "+ randomNumber)

What would you do to get a random number between 5 and 10?

randomNumber = 4+math.GetRandomNumber(6)

TextWindow.WriteLine("Your number is "+ randomNumber)

Conditional Logic

In programming, you can evaluate user inputted integers and strings against stored variables to incredible effect. Typical conditional logic commands include if, then, else. Typical logic would have the programmer checking either inputs or variables against some previous defined value or string to see what outcome should happen. It is important to remember

Usage of conditional logic in english terms can be seen as follows:

If (question) Then

 (do something)

ElseIf (other questions) Then

 (do something else)

Else

 (do something that isn't part of the IF or ELSEIF)

EndIf

So in practical terms:

TextWindow.WriteLine("Please enter a number")

number = TextWindow.ReadNumber() 'inputting a number from the user

if number<10 then 'your variable number is being weighed against the value of 10, the colon is crucial

TextWindow.WriteLine("Your number is <10") 'notice the indent, this means this line of code is UNDER the line above

elseif number > 10 then

TextWindow.WriteLine("Your number is >10")

else

TextWindow.WriteLine("Your number must be 10")

EndIf

Indenting is CRUCIAL in your development of a working program in all other languages than smallBasic. In smallBasic you can ask the program to format itself to help with your indentation

Simply highlight the block of code that contains sections that should be indented and ask smallBasic to format it for you

Try it yourself with the installed version. Copy/paste this into the client and choose FormatProgram

TextWindow.WriteLine("Please enter a number")

number = TextWindow.ReadNumber() 'inputting a number from the user

if number<10 then 'your variable number is being weighed against the value of 10, the colon is crucial

TextWindow.WriteLine("Your number is <10") 'notice the indent, this means this line of code is UNDER the line above

elseif number > 10 then

TextWindow.WriteLine("Your number is >10")

else

TextWindow.WriteLine("Your number must be 10")

EndIf

BOOLEAN LOGIC

You can ask equivalency in your conditions (as we saw in the example above)

Greater than (>), less than (<) and equal to (=) all apply when making these logic. Likewise the more complex looking "not equal to" (<>)

if number > 10 (number is greater than 10)

if number >=10 (number is greater than or equal to 10)

if number = 10 (if the number is equal to exactly 10)

if number <> 10 (if the number is not equal to 10) 

In other languages (say Python) that the only changes are when making equivalency (==) or not equal to (!=)

You can also string together multiple logic evaluations:

if number<10 and gamePlayLoop =1 then

elseif number=10 and randomNumber <>3 then

Loops

Sometimes we want to repeat lines of code over and over. These are called loops. Mostly beyond the scope of a grade 9 introduction to programming there are 2 types of loops that accomplish this. They are the For and While loops. 

'initializes the loop

For i = 1 To 10

'repeating code goes here

TextWindow.WriteLine(i)

EndFor

'exits when the answer is equal to 3

answer = 5

while answer <> 3

TextWindow.WriteLine("Please enter a number guess")

answer = TextWindow.ReadNumber()

endwhile

TextWindow.WriteLine("This shows the next line of code after the loop is now executed")

So how do we use structures over and over? Remember the CodeCombat game used While True. We'll do the same:

'notice the WHILE command below means it's going to a loop that happens until you 'break' it. We literally type in the code BREAK to make sure the code/game exits when that condition is reached


'everything has to be indented to be "inside" the while loop

break = "false"

while break = "false"

TextWindow.WriteLine("Enter a number please")

number = TextWindow.ReadNumber()

if number<10 then

  TextWindow.WriteLine("Your number is <10") 'what happens is if the number is less than 10 you get that response

elseif number=10 then

  TextWindow.WriteLine("Your number is equal to 10") 'if it's equal to 10 you get the prompt

else

  break="true" ' if it's larger than 10 the while loop breaks and you're into the statement below.

endif

EndWhile


TextWindow.WriteLine("You found the way out - something greater than 10")

'the print statement above works after the while loop is done because it's indented the same amount as the while statement


'note, you could have instead done break=0 and break =1 for false and true and it would be less typing in the long run

Activity:

Write a program that give the user feedback of "higher" or "lower" until the user correctly guesses the number between 1 and 20

Assignment #9: smallBasic questions

(if you need additional resources, there are many online)

Answer the following questions:

Click INVITE and get share links from Repl.it for your answers to turn in the assignment

Assignment #10: Python mini-game

Using the Repl.it website develop a simple game that is less ~ 500 lines of code long. Be sure to save the share-link so you can submit it when complete (or copy it into a N++ file). Easier games would be, say, a point-keeping trivia game. More complex games would be something like a combat simulator. Possible mark ranges vary based on the complexity of the game as well as type of game. Game ranking for the "game-off" will also factor into the final mark.

Exemplar for the structure for a 'simple game' (trivia)

Exemplar for the structure for a 'more complex game (adventure)'. Here's example 1, and example 2 and a (picture setup) for that. Or maybe a combat simulator

When complete submit the small basic file to Google Classroom