1 of 28

OpenBrackets

Beginning Programming

Week 3 Day 2

Welcome!

2 of 28

Welcome to class!

This is the Starting Programming class

  • 5 - 6:30pm : 5 min break towards the middle�
  • Teachers and TAs are:
    • Aidan, Kevin, Rohan, and Sean

  • No grades - but the more we can help each other learn as a group, the better

3 of 28

Have a question?

Ask a teacher or TA

Type in chat

Or

  1. Click Reactions
  2. Click Raise Hand

Or un-mute and speak up

4 of 28

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*

5 of 28

What does a list look like?

  • brackets [ ] can be used to create a list
    • when not behind a variable name!
  • values are separated by commas

my_list = [1,2,3]

my_list = ["a","b","c"]

my_list = ["a",2,"c"]

6 of 28

What can we do with a list?

We can:

  • keep items grouped together in one list
  • replace items in a list
  • look at specific items in the list

7 of 28

Let's build some characters

What is a character in a game?

  • bear?
  • robot?
  • ghost?

�What attributes would they have?

  • name?
  • emoji? 🦝 emojipedia.org
  • location?
  • mood?

8 of 28

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 ]

9 of 28

Better layout of rooms in INFINITE DUNGEON

Let's rewrite the INFINITE DUNGEON

  • Write each room as a list:
    • name of room
    • list of exits
    • where those exits go
    • anything in the room
  • Put those rooms in a big list of all the rooms
  • Print each room to check

We won't make a playable version ... yet ...

10 of 28

How can we iterate through a list

A faster way to loop through a list is using a for

  • A for loop automatically puts each value from a list into a variable, for each iteration of the loop

for a_variable in my_list:

print(my_list)

11 of 28

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)

12 of 28

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)

13 of 28

Better layout of rooms in INFINITE DUNGEON

  • Using our big list of all the rooms:
    • Use a for loop to print out each room's list
  • Then,
    • "start" the player in the first room and write code to print out all the possible exits
    • ask the player where they want to go
    • determine if they went into one of the exits
    • based on which exit, update which room they are in

14 of 28

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) ]

15 of 28

Can you draw with print() ?

You can print emojis as part of strings in repl

  • Find one here: https://emojipedia.org/nature/
  • Put it in quotes in a print() statement:

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?

16 of 28

* 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?

17 of 28

Can you make the bear move based on user input?

Write a program that:

  • asks the user for the column to print it in
  • prints the emoji in the column
  • loops again to ask the user for another column number

Can you instead ask for a direction to move?

18 of 28

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?

19 of 28

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

20 of 28

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?

21 of 28

Bears move in at least two dimensions

forest = [

['🐻','🌲','🌲'],

['🌲','🌲','🌲'],

['🌲','🌲','🌲'],

]

for row in forest:

for col in row:

print(col,end="")

print()

22 of 28

How do we move the bear around the forest

Different approaches:

  1. We can store the bear in the list of lists
  2. We can just store "where the bear is"

Where is the bear in these two examples?

[['🐻','🌲','🌲'],['🌲','🌲','🌲'],['🌲','🌲','🌲'],]

forest[0][0]

[['🌲','🌲','🌲'],['🌲','🌲','🐻'],['🌲','🌲','🌲'],]

forest[1][2]

23 of 28

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

24 of 28

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

25 of 28

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

26 of 28

When you need help outside class

Email openbracketscoding@gmail.com and say what class and teacher you have

  • Don't email teacher directly.

If you are age 13 or older, you can try Discord

  • https://discord.gg/ZRtk534

The age restriction is because of Discord’s Terms of Service

27 of 28

Reminder

No class next week

28 of 28

Thanks for coming to class!

Please let us know if you have any questions!