1 of 22

OpenBrackets

Intermediate Programming

Week 2 Day 1

Welcome!

2 of 22

Welcome to class!

This is the Intermediate Programming (in Python) class. Make sure you’re in the right place!

Schedule for today:

  • 60 minutes of instruction and exercises
  • 5 minutes for a break
  • 25 minutes for building a minigame

3 of 22

Introductions

Say hello to our teachers and TAs!

Keep your camera on as much as possible.

Don’t be afraid to ask questions!

4 of 22

Asking Questions

Type in chat

Or

  1. Click Reactions
  2. Click Raise Hand

5 of 22

How are you doing?

  1. From a scale of 1 to 10, how’s your day so far and why?
  2. How would you place the emotes on this scale? (From left to right, these emotes are named lemonangry, lemonpensive, lemonfrown, lemonthink, lemonyeah, and lemonwoke.)

1 2 3 4 5 6 7 8 9 10

6 of 22

Python Review: Part 2 (cont.)

Libraries, Lists, and 2D Lists

7 of 22

Libraries

  • Libraries are pieces of code that somebody else has already written for us
  • For example, we can use the random library to generate random numbers
  • To use a library, we need to import it

Exercises

  • Try printing a random number between 1 and 12
  • Use the math library to print the square root of each number from 0 to 99 (hint: use math.sqrt)

8 of 22

Lists

  • We can use lists to keep data together
  • A list can have any type of variable in it
  • We create a list by putting values inside square brackets, with commas between them
  • To access an item in a list, we use indexes and square brackets
    • Remember that indexes start from 0
      • my_list[2] accesses the third item
    • We can also use this to assign values in a list
      • my_list[2] = 5
  • We can assign values in a list in the same way we set variables

9 of 22

List Methods

  • We can do many different operations on a list
    • my_list.append(item)appends an item to the end
    • my_list.insert(0, item)inserts an item at a certain index
    • my_list.remove(item)removes an item by searching for the value
  • .remove, .append, and .insert are called methods
  • To get the length of a list, we can use the len() function
    • len(my_list)

10 of 22

List Exercises

  1. Create a list called numbers with the value [3, 0, "a", 4, 1, "b"]
  2. Remove the “a” using the remove function
  3. insert 12 in the second position of the list (after the 3)
  4. Print numbers
  5. append 5 to the end of the list
  6. Change the 0 to a 2
  7. Print numbers

11 of 22

2D Lists

  • Sometimes, we can have lists of lists
  • This is helpful for things like 2D grids
  • We can do the same operations since we still have a list
    • Just remember that the items of our large list are lists too
  • To get one row of the grid, we access the list with square brackets, as usual
  • Then to get one item of the grid, we access that row with square brackets again
    • my_2d_list[1][2]

12 of 22

2D List Exercises

  1. Create a 2D list called grid which is equal to:�[[1, 6, 3],� [2, 9, 4],� [5, 8, 7]]
  2. Print 4 by accessing it inside of grid
  3. Add a 4th row to the grid, with the values [0, 11, 10]
  4. Remove the 1st row of the grid

13 of 22

5 minute break!

  • Turn your camera off
  • Get a drink of water
  • Stand up and stretch

14 of 22

Python Review: Part 3

A potion price negotiation game

15 of 22

Exercise: Pseudocode Translation

16 of 22

Potion Price Negotiations

Build a game: barter with Hagglefluff the Discount Potion Brewer

  • Greet the player and offer a potion of ____ for sale (you decide)
  • Tell the player that the potion costs 100 yen, but Hagglefluff might accept less
  • Decide on a secret price that Hagglefluff will accept (a random number between 0 and 100)
  • The player should guess by offering a number
  • If the player's price is less than the secret price, ask them to offer a higher price
  • When the player finally buys the potion, tell them what the secret price was, and how much more money they had to pay than Hagglefluff would have accepted

17 of 22

Program Outline

  • Print an input greeting, and offer the player your potion
  • Tell the player the rules of the game
  • Generate a random secret price for the potion
  • Ask the player for their guess
  • Compare the guess to the secret price
    • If it’s less than the secret price, ask again
    • Otherwise, the player has bought the potion so stop looping
  • If player buys potion, report the secret price, and how much more they paid than the secret price

18 of 22

Extras: Dictionaries

19 of 22

Dictionaries

  • We can keep data together in other ways as well
  • Dictionaries hold key-value pairs
    • In a book dictionary, the word is the key and the definition is the value
  • We use square brackets to set and get values for a key as well
    • Earlier, we had lists where we put the index in the square brackets
    • Now, we’re putting a special key inside the brackets

20 of 22

Dictionaries Exercises

  • Create a dictionary called population equal to �{"United States": 328239523, � "California": 39512223, � "Washington": 7614893, � "Oregon": 4217737, � "Nevada": 3080156}
  • Print the population of Oregon
  • Add the population of Idaho, which is 1,787,065
  • Remove the United States from the population dictionary

21 of 22

When you need help outside class

Email contact@openbrackets.us with your class name, teacher, and question

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

22 of 22

Thanks for coming to class!

Please let us know if you have any questions!