Published using Google Docs
Teacher Programming Resource for Handouts 4 & 5- 2023
Updated automatically every 5 minutes

Teacher Programming Resource

Unit 4

Teacher Programming Resource for

Handouts 4 & 5

What’s the difference? (Handout 4)

Establishing variables and the print function (question 1)

There are two main things to notice in this question. First, we can create a variable by choosing a variable name and assigning a value to it (a string of text, an integer, etc.) using an equal sign. Notice that if you hover over a block before selecting to use it in EduBlocks, a brief description of the block appears. For instance, when hovering over the create a variable block, the message “assign a value, increment, or decrement value” appears next to the block.

Note that a key difference between a variable in mathematics vs a variable in programming is that, unlike in mathematics, a variable in programming is always assigned one value at a time. The value of a variable can be overwritten and changed within a program to something new. A good metaphor for a programming variable is that of a box. It can contain one thing at a time, and you can change what is inside of it throughout the program, if desired. A variable in mathematics can sometimes represent just one value (like in the case of x+1=7, where x=6) but also a set of values (for example, you can look at the graph of x+1 over all real numbers). Another key difference is that variables only represent numbers in mathematics, but in programming variables can be assigned to any set of numbers, letters, and/or characters.

In these programs, input() is used to assign a value to a variable. The input() function allows the program to intake input from the user. In this example, we store (or “assign”) the user’s input to the variable NumberOfSongsInPlaylist. Anytime the programmer wishes to intake input from the user, they must use the input() function. Inside the function you write the prompt that tells the user what to input.

Second, there are different types of print statements: with or without quotes. A print statement with quotes prints whatever is inside the quotes, in this case it is the variable's name. A print statement without quotes prints whatever information is stored in that variable (in this case, whatever the user typed in).

In general, quotes are used to indicate to Python that what’s inside of them is text.

Variable types and the + symbol (question 2)

There are 4 examples of code for students to run and observe. In the first one (a), students might be surprised to find that the numbers they enter will get concatenated rather than added. This happens because whenever Python takes in input (using the input() function), it saves it as a string. In example (b), the code works as they might expect. They may notice that the difference is the addition of the int() function to “re-cast” (i.e. change the type) of the variables. In example (c), they will see an attempt to use the power of concatenation to print the result in a single line, but since the variable “total” is an integer, and the other piece is a string (since it’s in quotes), the program fails. It is important to note that the error code for the program explains this. And finally, example (d) re-casts total as a string for successful printing of the result in one line.

This question introduces students to some complex but important concepts in programming. In Q1 students saw a variable defined and used, but even though nothing in Python explicitly indicates so, variables have “types” and these become important to how the variables are used. The main variable types used in this course will be “string” and “integer”. Integers, as you might imagine, are numbers, and strings are text (or any string of characters i.e.: abcABC123!@#$%^&* etc.). For example, if you store 8 as a string, the computer is storing the character 8 not the integer value 8.

In general, Python attempts to infer what type of variable you might want. For example if you include the line/block “variable_name = 3”, it will save it as an integer variable, but if you include “variable_name = ‘example’ ” it will save it as a string (as mentioned in Q1, quotes are a way to indicate strings). However, in this question, students will notice that sometimes you’ll need to tell Python what type to use for different variables, and even to convert them back and forth. This can be done using the functions “int()” and “str()”. Once you include that line of code/block with the name of your variable as the argument (i.e. in the parentheses), Python will understand that variable as the corresponding type. For example, if you have a variable named variable_1 and want to make sure Python understands it as a string rather than an integer, you can add “str(variable_1)” to your code.

One of the places where variable type is very important, is when the “+” symbol comes into play. When you try to “add” two integers, the symbol does what you expect, it adds them. However, if you try to “add” two strings, the + symbol “concatenates” them instead, which means it sets them side-by-side. For example, notice in the code below, how when we tell Python to read 2 and 7 as strings, the result of “adding” them is 27, whereas when we tell it to read them as integers, the result of the addition is 9:

 

Lists, indexing, and randomness (question 3)

There are three main concepts to notice in this question. First, we can create a “list” of items by selecting the “create a list” block and editing the variable name and contents. For this question the name of the list is “playlist” and the elements are “song1” through “song8” (in quotes to denote they are strings and not names of variables).

Second, this question introduces students to a way to select a random item from a list using the “random.randint” block. Whenever using a random function, you must also use the “import random” block, which is only required once in the program and typically placed at the top of code. Failing to use the import random block will result in an error message, as is the case in (a).

Tech Note: Python includes many basic functions, but some functions are only available if you import the library they come from. These functions are those that have a “.function after the library name, like random or math. For example, if you want to use certain math operations like square root, which is “math.sqrt,” you need to import the math library. The use of random.randint() is why we need to import random in this question.

Finally, the goal of this program is to generate a random integer, between 1 and 8, that corresponds with one of the 8 elements in the list in order to select a random song from our list. You can select an element using the “pick from a list” block and assign it an “index,” which is the number corresponding to each entry in the list. It might be helpful to think of it like the index of a book, which points you to the place where the information is stored in the book. It is important to note that the indexes begin with 0, meaning that the first entry in the list corresponds to an index of 0. For example, in the program below, we choose the first element in the list using 0 as our index and the third using index 2:

Instead of using an assigned number, however, we want to use a random integer. The issue in program (b) is that the random number generated does not match the song, as shown below, since the random integers are between 1 and 8, and the list has indexes 0 through 7.

To work around this issue, we must subtract 1 from our random integer so that the number generated corresponds with the song number in the list. For example, if the random integer generated is 1, then subtracting 1 from this number yields an index of 0, which corresponds to the first element in the list, as shown in (c).

What’s going on here? (Handout 5)

For loops, lists, and += (question 1)

This question asks students to look through three working programs to deduce what a for loop does. In general, for loops repeat the code inside of them multiple times. In our three examples, the number of times the code runs is determined by the value inside of the range() function (see technology note for other ways to determine the iterations in for loops).

The for loop in program (a) uses an integer inside the range() function to determine how many times to repeat the print statement inside the loop. The program prints “What’s up?” a total of 5 times because the integer given in the range() function is 5. We can think of the computer running this program line by line, but then returning to the for loop line multiple times until the range is reached. Since programs (b) and (c) use for loops while also introducing other new ideas, this program can help students understand for loops in their basic form.

Program (b) models counting by 2’s. First the program intakes input from the user (stored as an integer) to decide how many turns of counting by 2 it should take. The input from the user is assigned to the variable “turns”. Next, the program defines a variable “new_number” equal to zero to start with. It is important to notice that this variable is defined outside of the loop, otherwise the variable would be reset to zero each time the loop restarts and we would be unable to continuously increment the value. The for loop repeatedly overwrites the new_number to increase by 2 and print. There are two important features to notice in this loop. First, the variable “new_number” is being re-assigned a different value each time the loop runs. Second, the program is using the “+=” symbol to overwrite the value stored in “new_number” by adding 2 each time the loop runs. The “+=” symbol is a useful shorthand meaning new_number = new_number+2, which would be nonsensical if variables worked like they do in math, but is (surprisingly!) reasonable with programming variables. Put simply, “+= 2” takes the value stored in the variable, adds two, and stores this new value instead.  

In program (c), the for loop is used to add elements to a list. The program starts by asking the user how many numbers they want in their list, asking for a minimum of two, since at the end the program will print the second number in the list. An important thing to notice is that in line 3 we make an empty list called “number_list” in order to add things to it later using the list.append() function. This is just like when we create variables before using them. If students are confused about that line, encourage them to make a similar program without it and see what happens.

Each time the loop runs, it creates a variable called “new_number” by asking the user to input an integer to add to the list. Then the program adds that integer to “number_list”, by using “number_list.append()”, which adds to the end of the list whatever you put in parenthesis. This means that after each iteration of the for loop, the list grows by one entry.

Finally, the program uses the pick from a list block (“number_list[ ]”) to pull out the second element of the list and print it. As a reminder, it uses 1 for the index, since the list is indexed starting from 0, so the second item has index 1.

Interpreting Outputs (question 2)

The main takeaway students should have is that the two outputs shown are quite different from each other and the program will need to do different things depending on the input the user gives. This will be relevant in Q3, where they are introduced to the programming structure that allows for branching.

The question offers students a few ways to record their thinking: making a flowchart, jotting some notes or bullet points, writing a paragraph, making a list, etc. The idea is for students to summarize their thoughts in whatever way makes the most sense to them. If you would like, you could introduce students to the idea of pseudo code. Pseudo code is a useful technique for thinking about a program you might want to make. The prefix “pseudo-” means “not real” so the idea is to write “not real” code. What this means is to write the big picture ideas of what might go in your program but with the aim of other people understanding, rather than being readable by a computer, the technicalities of what functions or pieces of code to use can be dealt with later. A good start is to break down the goal of your code into smaller steps and write a sentence for each step. There are many ways of approaching pseudo code but this might help students get started.

Additionally, if students are feeling stuck on how the Pythagorean theorem can be embedded in code, having them come up with a few example triangles and work through solving for missing sides themselves can help them figure out the list of steps that one needs to take.

Coding the Pythagorean Theorem (question 3)

In this question students are introduced to if-else statements. As students likely intuited in Q2, in programs we often want to be able to branch into different code depending on some input and if-else statements allow us to do that. The general structure of an if-else statement has first the if statement, which checks if a certain condition is met, and if it is, it runs the code inside of it. If the condition is not met, then it runs the code inside the else statement instead.

Checking that a condition is true for the if statement is also a new idea. In this case, the condition is written using the == operator. Since the “=” symbol is used to assign values to variables, in order to check for equality we use “==”. The == operator checks that the two things on either side of it are identical to each other, so it can be used for numbers, but also for strings (as in this example). Alternatively, you can use other operators such as: not equal (“!=” ), less than (“<”), and less than or equal to (“<=”).

In this program, the branching comes into play because the pythagorean theorem can be used to solve for a missing leg or missing hypotenuse of a right triangle, and the way we substitute into the formula depends upon which type of side we are solving for. So, the program in Q3 needs to make a decision of whether to run what’s inside the if block (solving for a missing hypotenuse) or the else block (solving for a missing leg) block. To make this decision the program starts by asking the user a yes or no question about whether or not they know the length of the hypotenuse. The user's response is stored as “hypotenuse_response” and then read within the logic of the if-else statement to determine which block to execute.

Inside each of the two blocks the program asks the user for the measures of the two known sides, assigns them to variables (“leg_1” and “leg_2” for legs and/or “hyp” for hypotenuse), and uses them to calculate the missing part. The calculation was built using tools from the math menu: the “( ) + ( )” block (edited to insert the variables and change the sign to multiplication) and the “math.sqrt()” function. Remember that we must “import math” at the beginning of this program since “math.sqrt()” comes from the math library. Finally, the program prints the result.

This work is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/