2. Variables

Variables Lab

In this lab, we will learn about variables, a powerful way of storing and working with information. Start by opening a Terminal window and navigating to ~/Desktop/making_with_code/pedprog/unit00/lab02. If you don’t have a lab02 directory, run mwc update.

👾 💬
Once you have reached the lab02 directory, don’t forget to run poetry shell. If you ever want to leave the poetry shell, type exit and you’ll be back in the normal shell.

Variable tests

💻 Open the file `variables.py` by using the command atom variables.py. You should see:

1
2
3
4
5
6
7
8
9
from turtle import *

print("--- VARIABLE TESTS ---")
print()
print("-- [variable test 0]-- ")
name = "YOUR NAME"
print("Variable test 0")
print("Hello")
print(name)

Variable test 0

💻 Start by replacing "Your name" (line 6) with your name (but keep the ""). Now you have declared the name variable and assigned your name as its value.

💻 Save the file and run the program. You should see an output similar to the one below:

$ python variables.py
--- VARIABLE TESTS ---

-- [variable test 0]-- 
Variable test 0
Hello
Emma

What just happened? After storing your name in the name variable, print(name) prints out whatever is stored in the variable.

Let’s do another test.

💻 Add the following lines of code to your file.
10
11
12
name = "YOUR FRIEND'S NAME"
print("Hello")
print(name)

💻 Replace "YOUR FRIEND'S NAME" with your friend’s name.

Now name is declared twice (on lines 6 and 10), and it’s printed out twice (on lines 9 and 12). What do you think will happen?

💻 Run the code to find out! You should see an output similar to the one below:

~/Desktop/cs9/unit_01$ python lab_02.py
--- VARIABLE TESTS ---

-- [variable test 0]-- 
Variable test 0
Hello
Emma
Hello
Britte

Is this the output you expected? Talk with your group about what happened when you assigned the name variable twice.

✅ CHECKPOINT:
In your group, draw a visual representation in your notebook of what happened when you assigned the name variable twice.

Variable test 1

💻 Copy the code block below into your file. Replace "color" and "fruit" with your favorite color and fruit:

13
14
15
16
17
18
19
# [variable test 1]
print()
print("-- [variable test 1] --")
favorite_color = "color"
print("Your favorite color is " + favorite_color)
print("Your favorite fruit is " + favorite_fruit)
favorite_fruit = "fruit"

💻 Run the program again Hmm, something is wrong here. Work with your group to find and fix the bug.

✅ CHECKPOINT:
In your notebook, describe the bug and how you fixed it.

Variable test 2

💻 Copy the code block below into your file
20
21
22
23
24
# [variable test 3]
print()
print("-- [variable test 2] --")
favorite_artist = input("What is your favorite artist? ")
print("Oh, I love " + favorite_artist + "!")
💻 Run your program multiple times and change up what artist you type.

This shows how your programs can be responsive to user input and how you can store information from the user in variables that may change every time your program runs.

✅ CHECKPOINT:

Answer the following check-in questions in your notebook before moving on.

  1. What is a variable?
  2. How do you declare a variable?
  3. At what point in a program can you use a variable?

Responsive Drawing

The last variable test showed how your programs can be responsive to user input and how you can store information from the user in variables that may change every time your program runs.

This means that we can use variables to make our code do different things at different times based on input. Can you imagine how that might help use make more interesting turtle drawing?

💻 In a new file, create a turtle drawing that is responsive to user input. Create your drawing in a file called variable_drawing.py. You might want to start with some code like this:

1
2
3
4
5
6
7
from turtle import *

scale = int(input("How big should it be?"))
forward(scale)

# Put this at the end of your program to keep the drawing window open
input("Press enter to continue...")
👾 💬 FYI

You can get input from the user while your program is running using input("PROMPT").

If you want to get a number from the user (specifically an integer), use int(input("PROMPT")). The int function turns a word into a number. (That’s all well and good when the word is something like “101”, but see what happens when you type in something like “popsicle”.) We’ll talk more about all this next unit.

Extension

Scale a Drawing

💻 In a new file, create a turtle drawing of a face that changes sizes based on a user inputed size factor. Create your drawing in a file called variable_face.py.

For example, if the user inputs 1 the face should be small. If the user inputs 2, the face should be twice as large. If the user inputs 3, the face should be three times as large.