Variables & Assignments
Breadcrumb: /csp/big-idea-3/variables_assignments/p3This page will teach you about variables and assignments in programming.
Variables & Assignments
- Introduction:
- Variables are ways to store values inside a game or code for later use. It can be numerical or strings(letters and words). It is like a box that has a name but the contents inside cannot be changed. Make sure you name your variables with descriptive, but not long words.
- Key concepts:
- Variable: A container that stores information and values
- Assignment: The value being assigned to the variable
- Value: The data inside a variable
Popcorn Hack 1
# Example: snakeSize = 10
snakeSize = ? # <-- Add the size of the snake
snakeColor = "" # <-- Add the color of the snake
# Print greeting
print("The snake is " + str(snakeSize) + " inches long and it's " + snakeColor + ".")
3. Python:
age = 15 # Stores "age" as 15, INTEGER
weight = 135 # Stores "weight" as 135, INTEGER
name = "Mike" # Stores "name" as Mike, STRING
JavaScript
(Similar to python)
let age = 15 // Stores "age" as 15 as well, but add let in front
let weight = 135 // Stores "weight" as 135
let name = "Mike" // Stores "name" as Mike
WARNING! Avoid doing the follwing:
let age == 14 // Incorrect as "==" is for comparing values
age = 14 // does not create a variable in JavaScript
Popcorn Hack 2
# Define the win/lose messages first
messageWin = "" # <-- Change this to "You win!"
messageLose = "" # <-- Change this to "You lose!"
# Choose outcome
winOrLose = "" # <-- Change this to either "win" or "lose"
# Print result
if winOrLose == "win":
print(messageWin)
else:
print(messageLose)
Which one do you think is a better variable name?
- highScore vs highestScoreInTheGame
- firstName vs 1stName
- ID vs studentID
Multiple Choice!
Compete as teams and try to win!