3.01 Variables HW
Homework submission for OCS Python 3.01 Variables
Popcorn Hack · Checkpoint challenge
Look at the code comments for the explanation. Prediction: Stars = 12, Checkpoint Stars = 8
Code Runner Challenge
Popcorn Hack 1
View IPYNB Source
%%python
# CODE_RUNNER: Popcorn Hack 1
stars = 8
checkpoint_stars = stars # Python runs line by line, so this captures the value of stars at this point (8)
stars = stars + 4 # Increment stars by 4
print(stars) # 12 - stars was incremented by 4 from its initial value of 8
print(checkpoint_stars) # 8 - checkpoint_stars captured the value of stars before it was incremented
MCQ
Answers: B, A, C, B Score: 4/4
Homework Hack · Build your UESL star counter
Code Runner Challenge
Homework Hack
View IPYNB Source
%%python
# CODE_RUNNER: Homework Hack
game_name = "Star Trail"
reduced_motion = True
level_one_stars = 12
level_two_stars = 18
level_three_stars = 10
total_stars = level_one_stars + level_two_stars + level_three_stars
print(f"Game Name: {game_name}")
print(f"Setting: Reduced Motion? {reduced_motion}")
print(f"Total Stars: {total_stars}")
bonus = 5
level_two_stars = level_two_stars + bonus
print("Old Total:", total_stars)
total_stars = level_one_stars + level_two_stars + level_three_stars
print("New Total:", total_stars)
Tests for Stars
Bonus 5 Prediction: Original is 40, Stored 40, Recalculated 45
Bonus 0 Prediction: Original is 40, Stored is 40, Recalculation is 40.
Code Runner Challenge
Homework Hack Tests
View IPYNB Source
%%python
# CODE_RUNNER: Homework Hack Tests
for bonus in [5,0]:
level_one_stars = 12
level_two_stars = 18
level_three_stars = 10
total_stars = level_one_stars + level_two_stars + level_three_stars
print("Old Total:", total_stars)
level_two_stars = level_two_stars + bonus
total_stars = level_one_stars + level_two_stars + level_three_stars
print("New Total:", total_stars)
JavaScript Tracing · Checkpoint Stars
This JavaScript example performs the same checkpoint tracing as the Python Popcorn Hack. It demonstrates how variables store values and how reassignment affects them.
let stars = 8;
let checkpointStars = stars;
stars = stars + 4;
console.log(stars);
console.log(checkpointStars);
Trace:
| Statement | stars |
checkpointStars |
|---|---|---|
let stars = 8 |
8 | Not assigned yet |
let checkpointStars = stars |
8 | 8 |
stars = stars + 4 |
12 | 8 |
Predicted output:
12
8
Explanation: JavaScript executes statements sequentially, just like Python. When checkpointStars is assigned the value of stars, it stores the current value (8). Increasing stars afterward does not change the value stored in checkpointStars.
JavaScript Tracing · Star Bonus
This example uses a loop to test two different bonus values, similar to the Python star counter tests above.
for (let bonus of [5, 0]) {
let levelOneStars = 12;
let levelTwoStars = 18;
let levelThreeStars = 10;
let totalStars = levelOneStars + levelTwoStars + levelThreeStars;
console.log("Old Total:", totalStars);
levelTwoStars = levelTwoStars + bonus;
totalStars = levelOneStars + levelTwoStars + levelThreeStars;
console.log("New Total:", totalStars);
}
Predicted output:
Old Total: 40
New Total: 45
Old Total: 40
New Total: 40
Explanation: The loop runs twice, once with a bonus of 5 and once with a bonus of 0. The original total is 40 in both cases. The total must be recalculated after changing levelTwoStars, because changing one variable does not automatically update another variable that previously stored a calculation.
Cross-Language Comparison · Python vs. JavaScript
Python and JavaScript both use variables to store values, perform calculations, and update information. However, their syntax differs.
| Concept | Python | JavaScript |
|---|---|---|
| Creating a variable | stars = 8 |
let stars = 8; |
| Creating a constant | No direct equivalent using ordinary assignment; uppercase naming is a convention | const stars = 8; |
| Boolean true | True |
true |
| Boolean false | False |
false |
| Printing output | print(stars) |
console.log(stars); |
| Adding to a variable | stars = stars + 4 |
stars = stars + 4; |
| String formatting | f"Stars: {stars}" |
`Stars: ${stars}` |
Similarities:
- Both languages execute statements in order.
- Both allow variables to be reassigned.
- Both support numerical calculations and Boolean values.
- Both store the value assigned at the time of assignment. Changing one variable does not automatically change another variable that copied its value.
Differences:
- Python does not require
letorconstwhen creating variables, while JavaScript uses declarations such asletandconst. - Python capitalizes Boolean values (
TrueandFalse), while JavaScript uses lowercase (trueandfalse). - Python uses
print()to display output, while JavaScript commonly usesconsole.log(). - Python uses f-strings for convenient string formatting, while JavaScript supports template literals.
Reflection: Although Python and JavaScript have different syntax, their underlying variable behavior is similar. Both languages allow a variable to store a value, and both require recalculating a total when one of the values used in that calculation changes. Learning both languages helps me recognize programming concepts independently of their syntax.
Design Thinking
Player Need: Some players may be uncomfortable or face serious health effects such as epilepsy from fast movement or animation.
Goal: Give the player freedom over full motion or reduced motion while keeping the game’s functionality
Representation Considered: A boolean is useful for reduced motion because the setting is simply either On or Off (True or False). Numerical variables work well for Star counts as those are numerical values.
Prototype: The star counter stores three star counters for each level, a total score, and the reduced motion boolean setting.
Test/Revision: Testing made me realize that the stored total must be recalculated afer each level value changes. Reduced motion should be a player choice as each player differs; Some players may need/prefer it while others may not.