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 thelab02
directory, don’t forget to runpoetry shell
. If you ever want to leave the poetry shell, typeexit
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:
|
|
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.
|
|
💻
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 thename
variable twice.
Variable test 1
💻 Copy the code block below into your file.
Replace "color"
and "fruit"
with your favorite color and 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
|
|
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.
- What is a variable?
- How do you declare a variable?
- 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:
|
|
👾 💬 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"))
. Theint
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.