1 of 16

OpenBrackets

Beginning Programming

Week 6 Day 2

Welcome!

2 of 16

Welcome to class!

This is the Beginning Programming class

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

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

3 of 16

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 16

What is a function?

  • Functions are blocks of code that run when you call them
    • Often do “tasks”, making them reusable

Functions:

  • are defined with def, (parameters), and a :
  • indented code is the code run when the function is called
  • functions have arguments/parameters - values you put in
  • functions return values back at the end

Will not do anything unless we call the function at the bottom with ()

def printName():

name = "Trisha"

print(name)

printName()

5 of 16

Libraries are great ways to get more functions

  • Collection of resources someone else made (like a toolbox, or a script)
  • You can access them by importing [library]

Example: the random library

  • Has something to find a random number between 2 given numbers
  • Saves YOU time, you don’t have to write this

Import random * # This imports all the

# stuff from random

print(random.randint(0,10)) # This uses randint from

# the random library

6 of 16

Bear attributes

How do we store properties about a bear? We could use a list with certain positions:

bear_attributes = [ 'frank', 500, 'insect hive removal specialist' ]

If we use a list we must:

  • be careful to put values in the right order
  • comment which value is where so we don't confuse them

7 of 16

Bear attributes

Another way is to use a dictionary :

bear_attributes = { 'name': 'frank', 'weight': 500,

'occupation': 'insect hive removal specialist' }

bear_attributes['occupation']

  • Dictionaries use curly braces to define them
  • Dictionaries use a string as a key
  • You can use a string as a key to get a value

Dictionaries are like lists, but you use a string to access which value, not an integer

8 of 16

Examples of dictionary

this_dictionary = { 'pocket' : 'lint' }

print(this_dictionary)

this_dictionary['pocket'] = 'compass'

print(this_dictionary)

this_dictionary['belt'] = 'sliderule'

print(this_dictionary)

9 of 16

5 Minute Break!

10 of 16

Giving bear more attributes in the GRID FOREST

Let's modify the GRID FOREST to save all the bear information in a dictionary, with keys like:

  • row
  • column
  • hunger
  • tiredness

Modify the game to use these values from the dictionary

11 of 16

Second bear in the forest???

Next, let's add more bears to the forest - copy and paste the code you used to make the first bear, to make the second.

Let's have this be a PHASE-BEAR :

  • a PHASE-BEAR moves to a random position each turn

Store each of the bear's attribute dictionary in a list of all bears (list of dictionaries)

12 of 16

You can use dictionaries to translate strings to numbers

Dictionaries are particularly useful for turning n e w s�into numbers

  • Revist your code for INFINITE DUNGEON
  • Instead of using a for loop and counter to determine which exit was selected and where it goes,�use a dictionary where �the key is the direction�and the value is which room it leads to

You can use a similar trick in GRID FOREST

13 of 16

Simple code is good

Use functions, dictionaries, lists, and for loops to make your code simple and short

Short is good�(except - more comments the better)

14 of 16

What games are you planning to make?

15 of 16

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

16 of 16

Thanks for coming to class!

Please let us know if you have any questions!