OpenBrackets
Beginning Programming
Week 3 Day 2
Welcome!
Welcome to class!
This is the Starting Programming class
Have a question?
Ask a teacher or TA
Type in chat
Or
Or un-mute and speak up
Lists are a variable that stores a lot of values
Lists let you store many things in one variable
Lists let your program put and access values from positions *using a number to say which one*
What does a list look like?
my_list = [1,2,3]
my_list = ["a","b","c"]
my_list = ["a",2,"c"]
What can we do with a list?
We can:
Let's build some characters
What is a character in a game?
�What attributes would they have?
Lists can contain more lists!
# Name, color, number of spikes�monster_1 = ['barbara', 'purple', 12]�monster_2 = ['mo', 'cyan', 3]
moster_list = [ monster_1 , monster_2 ]
Better layout of rooms in INFINITE DUNGEON
Let's rewrite the INFINITE DUNGEON
We won't make a playable version ... yet ...
How can we iterate through a list
A faster way to loop through a list is using a for
for a_variable in my_list:
print(my_list)
for loops are often easier to write than while loops
my_list = ['a','b',3]
position = 0
while position < len(my_list):
print(my_list[position])
position = position + 1
my_list = ['a','b',3]
for value in my_list:
print(value)
You can nest for loops
Can we list every possible combination of fur colors and spikes for the monsters these?
You can put for loops inside of for loops...
for fur in my_list_fur_color:
for spikes in my_list_spikes:
print(fur,"with",spikes)
Better layout of rooms in INFINITE DUNGEON
You can add to and delete from a list
Sometimes you don't know how long your list needs to be
You can add to a list like you add strings:
monsters = monsters + [new_monster]
You can delete from a list using del and the number, like
del monsters[0]�del monsters[2]�del monsters[ len(monsters) ]
Can you draw with print() ?
You can print emojis as part of strings in repl
print("this is an emoji 🙃🐻")
Can you use this to draw a "person fencing" (or other emoji) on the screen?
Can you print them in different positions?
* repeats things
You can print lots of bears with
print("🐻"*10)
...you can also print many spaces and then combine it with a bear...
print(" "*9 + "🐻")
How do you use this to draw one (1) bear in the 5th column?
How do you use this to draw one (1) bear in the bear_position column?
Can you make the bear move based on user input?
Write a program that:
Can you instead ask for a direction to move?
Bears move in multiple dimensions
Can you print the bear in multiple dimensions?
How do you store this?
How do you print this?
What if the background is trees, not spaces?
sidenote about print
print() automatically presses enter each time
You can disable that by adding the argument end=""
print("this")
print("this", end="")
Try it out
Bears move in at least two dimensions
You can use a list of lists to store a map
You can put [] lists inside other [] lists
forest = [
['🐻','🌲','🌲'],
['🌲','🌲','🌲'],
['🌲','🌲','🌲'],
]
What happens if you try to print this?
Bears move in at least two dimensions
forest = [
['🐻','🌲','🌲'],
['🌲','🌲','🌲'],
['🌲','🌲','🌲'],
]
for row in forest:
for col in row:
print(col,end="")
print()
How do we move the bear around the forest
Different approaches:
Where is the bear in these two examples?
[� ['🐻','🌲','🌲'],� ['🌲','🌲','🌲'],� ['🌲','🌲','🌲'],� ]
forest[0][0]
[� ['🌲','🌲','🌲'],� ['🌲','🌲','🐻'],� ['🌲','🌲','🌲'],� ]
forest[1][2]
Let's draw the forest first, then put a bear in it
How do you make and print a forest variable of
forest_rows = 3
forest_cols = 3�?
range() is useful
len() is useful
empty_map = [[]]*5 # A list of five empty lists
for i in range(len(empty_map)):� print(i)
empty_map[i] = ['🌲']*5
�
Drawing the bear in the forest - just from "where it is"
bear_row = 1
bear_col = 1
How do we draw the "empty_map"?
Before that, how do we put a bear in a place in the map
How do you draw the bear in a new position?
Add to your code - ask for the bear's position as input,
then draw a forest with the bear at the new position
Then, put it in a loop
When you need help outside class
Email openbracketscoding@gmail.com and say what class and teacher you have
If you are age 13 or older, you can try Discord
The age restriction is because of Discord’s Terms of Service
Reminder
No class next week
Thanks for coming to class!
Please let us know if you have any questions!