Difference between revisions of "Module Three: Coding to Learn and Learning to Code"
(→Some Fundamental Coding Concepts)
(→Some Fundamental Coding Concepts)
|Line 104:||Line 104:|
When using comparison operators, the answer to each comparison will either be "true" or "false". For example, 5 < 10 would be ''true'', while 6 > 10 would be ''false''. When coding, we would say that a "comparison statement ''returns'' a ''boolean'' value of true or false". ''Returns'' just means the program has given a result
When using comparison operators, the answer to each comparison will either be "true" or "false". For example, 5 < 10 would be ''true'', while 6 > 10 would be ''false''. When coding, we would say that a "comparison statement ''returns'' a ''boolean'' value of true or false". ''Returns'' just means the program has given a result a certain segment of code.
Revision as of 23:32, 4 May 2020
What is it Actually Like to Code?
Coding is a bit like trying to have an instructive conversation with a young child who does not speak the same languages as you, and you only have a vague idea of what language she does speak. Unlike the child, however, the computer will not be trying to figure out a way to communicate back with you.
My uncle used to love to tell me that "a computer is nothing more than a fast idiot". Things have changed a lot in the technology landscape since then, but when it comes to the type of coding we'll do in this course, and what most people are likely to learn, this is still resoundingly true.
Computers will do what you instruct them to do, exactly.
Have you ever worked with a child who followed your explicit instructions to the letter, with a result that was inconsistent with what you actually meant? This is not an unusual result when you are writing a computer program. It's not that computers are trying to be maliciously compliant, it's that they do not make any allowances for cognitive leaps or metaphorical speaking, in any sense. If you tell a computer to drive 50mph south, it will drive south at 50mph no matter what obstacles appear in the way, no matter how many stop signs appear, no matter what bodies of water stand between it and due south.
Computers will ONLY do what you instruct them to do.
When you write instructions for a computer, you have to be incredibly meticulous, and cover every single step in a decision-making process to get the results you want. The computer will not infer anything that is not explicitly written, and it won't bridge a gap if you neglect to explain a part of the thinking process.
If you've ever tried to convince a teenager to clean their room, you may understand exactly what I mean here. They may have "cleaned their room", but perhaps the silverware never made it into the dishwasher. They may have "cleaned their room", but their dirty laundry may have been shoved under the bed instead of into the laundry hamper.
To get the result you want out of a computer program, you have to include absolutely everything you want it to do from beginning to end, without leaving anything out.
Computers cannot overlook typos, misplaced parentheses, or other syntactical errors.
To a computer program, "code" and "Code" are two different words because a capital C and a lower case c are not the same character. Similarly, a colon and semicolon are different characters. A computer program will not run if 99.9% of the code is written correctly, because it will get stuck on the place where you left an extra space or didn't indent to the correct degree. The vast majority of problems I've had to fix when writing programs were typos, most of which were absolutely tiny and therefor took forever to find.
Fortunately, modern coding programs (called IDEs, or Integrated Development Environments) make it a lot easier to find the errors in your code!
A lot of trial, and nearly as much error.
Having worked with a lot of programmers over the last decade or so, I can tell you that the trial and error process in coding never really ends, no matter how long you've been doing it. Languages are constantly being updated and expanded, which languages are in popular use is always changing, and since no two pieces of software are the same, the types of problems you'll try to solve with programming will always be new and different.
In the beginning, new coders tend to assume that they're fighting with their code because they're just not good at coding, and you may hear this from students a lot. In reality, though, coding means solving problems in collaboration with a partner that is inflexible and unwilling to meet you half way.
When frustration hits, and it will!, the best option is to take a break and come back later with a clear head to try again.
Here's a video of me fighting with my code, in case you don't believe me.
Video: The Struggle
What is it like to Learn to Code?
Learning to code is a lot like coding itself in that there is likely to be a lot of trial and error in the process of learning and becoming comfortable with coding. Specifically, to learn how to code in a regular coding language, there are two separate parts that need to be mastered,
- The fundamental concepts of coding, some of which are introduced below, and
- The syntax of the language you are learning to write code in.
In the beginning, it can be very difficult to tell which part you are struggling with as you create your first projects. This is part of the reason that block coding platforms (like Scratched) were developed to help new coders! We'll talk about Scratch more in the next section!
For now, I recorded this video to talk more about what you can expect when you're learning to code!
Video: What to Expect When Learning to Code
Some Fundamental Coding Concepts
In math, we use variables in the place of missing information in an equation. We do this in coding, too, but coding uses variables in a wider variety of ways than math does. We can use lots of different kind of information in coding, not just numbers!
Let's say that I want to make a computer version of Rock, Paper, Scissors where a user competes against the computer. At some point in my game, I will need to keep track of what option the computer chose, and which option the user chose. In order to have my program remember this information long enough for the game to run, I need to save it somehow. This is one aspect of what variables do - they allow us to remember a piece of information by assigning a name to it. For example, I can create a variable named user_choice to save the option that the user entered when playing my game, and I can save the computer's choice to a variable named computer_choice.
Once I have my user and computer choices saved, I can then compare them to figure out who won the game. This is the second aspects of what variables do in computer programs - they allow us to write instructions for a process when we don't know what the real information is going to be at any given moment. In my Rock, Paper, Scissors game, for example, I don't know what the user or the computer will pick when I'm writing the game, so I can't write my code with those details. Even if I could write my code that specifically, my game would fail if either the user or the computer made a different choice!
Instead, I have to write code that will work regardless of what choices the user and the computer make. This is an example of an abstraction! Instead of saying "the user chose rock and the computer chose paper, therefor the computer wins", I have to create a set of steps to allow the program to determine who wins in multiple possible scenarios! Variables make this a lot easier, because I can use them to insert the real values into the decision-making algorithm when the game is actually run.
Play to Learn
This is a Rock, Paper, Scissors game I built in Python. Click the link to open the game, then click the green Run button at the top of the page, and follow the instructions in the right-hand column to play. I love using Python and teaching it to kids because it's fairly readable, as programming languages go. Take a look at the code in the middle section of the page and see if you can follow what's going on! You can also check out the expanded version I made with students, Rock, Paper, Scissors, Lizard, Spock!
Data types are the different kinds of information that we can save in our variables. Generally, these include things like:
- Integers (numbers without a decimal)
- Floats (numbers with a decimal)
- Strings (any string of characters, usually words or sentences, such as the user_choice and computer_choice examples above)
- Lists (which contain more than one value, which can belong to different data types)
- Booleans (which are either/or variables, which are set to either "true" or "false")
I'm sure you've used comparison operators already, even if you were a K-12 student the last time you did! Comparison operators are symbols we use in coding to compare two different pieces of information. Here are a few examples:
|Operator Symbol||Operator Name||What it Does|
|<||"Less Than"||Compares two numerical values to determine if the value on the left is less than the value on the right.|
|>||"Greater Than"||Compares two numerical values to determine if the value on the left is greater than the value on the right.|
|<=||"Less Than or Equal To"||Compares two numerical values to determine if the value on the left is less than or equal to the value on the right.|
|>=||"Greater Than or Equal To"||Compares two numerical values to determine if the value on the left is greater than or equal to the value on the right.|
|==||"Equal To"||Compares two numerical values to determine if their values are the same.|
|!=||"Not Equal To"||Compares two numerical values to determine if their values are different.|
When using comparison operators, the answer to each comparison will either be "true" or "false". For example, 5 < 10 would be true, while 6 > 10 would be false. When coding, we would say that a "comparison statement returns a boolean value of true or false". Returns just means the program has given us a result for a certain segment of code.
Block Coding in Scratch
What is Scratch?
Signing up for a Teacher's Account
Tour of a New Scratch Project
Your First Scratch Project!
Create a Story Video Tutorial