02. Variables

Variables Lab

In this lab, we will learn about variables, a powerful storage container of information.

[0] Variable tests

๐Ÿ’ป Start by opening the Terminal cloning this lab onto your laptop. As a reminder, we will run this command at the start of each lab.

mwc update
๐Ÿ’ป In the Terminal, type the following command to open the lab folder.
cd ~/desktop/making_with_code/cs9/unit00_drawing/lab_variables
๐Ÿ‘พ ๐Ÿ’ฌ Windows Users
I suggest not copying the path command, and instead using cd and ls to ensure you are in the correct making_with_code folder.

๐Ÿ’ป Enter the Poetry Shell. We will also run this command at the start of each lab, but only when we are inside a lab folder.

poetry shell
๐Ÿ‘พ ๐Ÿ’ฌ Exiting the poetry shell
When you want to exit the shell, you can type exit or ^D

๐Ÿ’ป Take a look at the files inside with: ls

  • variable_test0.py
  • variable_test1.py
  • variable_test2.py
  • responsive_drawing.py

[Variable test 0]

๐Ÿ’ป First let's start by running variable_test0.py.

python variable_test0.py

You should see words print out in the Terminal.

๐Ÿ’ป Let's see how that is happening by opening the file in Atom: atom variable_test0.py

Atom is the code editor we will be using throughout this course.

#####################
# Unit 0 Lab 2
# variable_test0.py
#####################

name = "YOUR NAME"
print("Hello")
print(name)

๐Ÿ’ป Start by replacing "YOUR NAME" 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 again. You should see an output similar to the one below:

Tip: Use the up arrow to cycle through your previous commands in Terminal

$ python variable_test0.py
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.
name = "YOUR FRIEND'S NAME"
print("HELLO")
print(name)

๐Ÿ’ป Replace "Friend's name" with your friend’s name.

Now our program is printing the name variable twice but we’ve assigned different values to the variable at different places in the code. What do you think will happen?

๐Ÿ’ป Run the code to find out! You should see an output similar to the one below:

$ python variable_test0.py
Hello
Emma
Hello
Britte

[Variable test 1]

๐Ÿ’ป Let's continue explore variables by running a different variable test: python variable_test1.py

Hmm, something is wrong here.

๐Ÿ’ป Open up the code and try to fix the bug: atom variable_test1.py

#####################
# Unit 0 Lab 2
# variable_test1.py
#####################

favorite_color = "color"
print("Your favorite color is " + favorite_color)
print("Your favorite fruit is " + favorite_fruit)
favorite_fruit = "fruit"

[Variable test 2]

๐Ÿ’ป Now, let's experiment with user input. Run the 3rd variable test: python variable_test2.py

#####################
# Unit 0 Lab 2
# variable_test2.py
#####################


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.


[1] 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?

๐Ÿ’ป Create a turtle drawing that is responsive to user input. Open the file responsive_drawing.py to get started.

๐Ÿ‘พ ๐Ÿ’ฌ FYI

You can get input from the user while your program is running using input("PROMPT"). By default, input("PROMPT") collects words.

If you want to get a number from the user, use int(input("PROMPT")). This is because Python treats numbers and words differently. We’ll talk more about this next unit.

For example:

age = int(input("How old are you?"))

[Deliverables]

โšกโœจ
Once you’ve successfully completed the responsive drawing be sure to fill out this Google form.

[2] Extension: Size Factor Drawing

Create a turtle drawing of a face that changes sizes based on a user inputed size factor.

For example, if the user inputs 1 the face should be an average size. If the user inputs .5, the face should be half size. If the user inputs 2, the face should be twice the size.

๐Ÿ’ป Create a new file: atom size_factor.py.

Make note of how we create a new file. atom <file_name.py>