OpenBrackets
Beginning Programming
Week 6 Day 2
Welcome!
Welcome to class!
This is the Beginning Programming class
Have a question?
Ask a teacher or TA
Type in chat
Or
Or un-mute and speak up
What is a function?
Functions:
Will not do anything unless we call the function at the bottom with ()
def printName():
name = "Trisha"
print(name)
printName()
Libraries are great ways to get more functions
Example: the random library
Import random * # This imports all the
# stuff from random
print(random.randint(0,10)) # This uses randint from
# the random library
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:
Bear attributes
Another way is to use a dictionary :
bear_attributes = { 'name': 'frank', 'weight': 500,
'occupation': 'insect hive removal specialist' }
bear_attributes['occupation']
Dictionaries are like lists, but you use a string to access which value, not an integer
Examples of dictionary
this_dictionary = { 'pocket' : 'lint' }
print(this_dictionary)
this_dictionary['pocket'] = 'compass'
print(this_dictionary)
this_dictionary['belt'] = 'sliderule'
print(this_dictionary)
5 Minute Break!
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:
Modify the game to use these values from the dictionary
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 :
Store each of the bear's attribute dictionary in a list of all bears (list of dictionaries)
You can use dictionaries to translate strings to numbers
Dictionaries are particularly useful for turning n e w s�into numbers
You can use a similar trick in GRID FOREST
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)
What games are you planning to make?
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
Thanks for coming to class!
Please let us know if you have any questions!