1 of 27

OpenBrackets

Beginning Programming

Week 2 Day 1

Welcome!

2 of 27

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 27

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 27

Previously on OB...

  • Setup repls
  • Printed out a joke
  • Got user input
  • Saved values in variables
  • Made some calculators with those values

5 of 27

Remember you can store values in variables

Variables have a name

Like labelling a new place on a bookshelf

Don't put the name in quotes, just put something in that place

6 of 27

Operators are for doing math (and more) with values

A small portion of code is called an "expression"

Here's some more expressions to try --->

1 + 1

55 / 11

3*2

10 - 611

1000000 * 1000000

Symbols like + / - * are called "operators"

7 of 27

More ways to share - you can do "multiplayer" on repls

  1. click here

2) Then share the link

8 of 27

Group exercise - write MadLib code

  • Let's write a program that asks the user for some values, like: a_mood # like thrilled� an_animal # like a wombat� a_place # like a hair salon� an_activity # like skiing� a_number # like four
  • After taking inputs, print() out the story,� for example:�"I was thrilled to see a wombat in my local hair salon, because I normally only see four of them when they are skiing."

9 of 27

Values and operators can be grouped with ()

2 + 2 * 3

( 2 + 2 ) * 3

4 * 3

( 2 + 2 ) * ( int(input()) + 1 )

10 of 27

Some more types

Can you make one of each of these, and test with type()?

11 of 27

Some operators work with int and str values

You can combine strings using the + operator

How can you combine a str and an int?

You can convert any int into a str using str() function

type(12)

type(str(12))

"there are " + 12

"there are " + str(12)

You can convert any str into a int using int() function

"there are " + str( 12 + int("12") )

12 of 27

Group exercise - write a Time Factorializer

Ask the user to enter their age in years.�Print out:

  • The number of months that is
  • The number of weeks that is
  • The number of days that is
  • The number of hours that is
  • The number of minutes that is
  • The number of seconds that is

Make sure to print out text labelling which is which. Work together!

13 of 27

The Error-o-dex

  • print 5 → SyntaxError: Missing parentheses in call to 'print'. � Did you mean print(5)?
  • print (1+*2) → SyntaxError: invalid syntax
  • Print (1+2) → NameError: name 'Print' is not defined
  • a/2 → NameError: name 'a' is not defined
  • "2"/2 → TypeError: unsupported operand type(s) � for /: 'str' and 'int'
  • print(5 + 'a') → TypeError: unsupported operand type(s) � for +: 'int' and 'str'
  • sentence = "hi → SyntaxError: EOL while scanning � string literal

14 of 27

5 minute break!

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

15 of 27

Let's actually make a game!

Carl the Goblin is trying to "liberate" an book of spells from a private library guarded by trolls

...however...

Carl is not a warrior

Carl does know some illusion spells though!

Can Carl make the right choices to sneak in and get the book?

16 of 27

Logical operators can check if values are equal or not

  • = assigns the value on the right to the variable on the left�BUT
  • == is used to check whether the left and the right side have the same value
  • != is used to check if the left and the right side have a different value

Operators like == and != return values like True and False

These are called "boolean" or "logical" values

== and != are operators

17 of 27

You can test if values are equal - or not !

choice = "A"

print( choice )

print( choice == "A" )

print( "choice is equal to A: " + (choice == "A") )

print( "choice is equal to A: " + str(choice == "A") )

print("choice is not equal to A: "+str(choice != "A"))

print(type(choice != "A"))

18 of 27

bool aka boolean aka logical

type(True)

type(False)

True == True

True == False

False == False

19 of 27

Reminder:

You can test things out in the console

Click in Console, type, and press enter

Difference: everything is "print"-ed out to you�'Run' code is not automatically "print"-ed

20 of 27

Conditionals are how your program "thinks"

  • Conditionals allow the code to make decisions based on bool values like True and False
  • The most basic is an if statement

For examples, try these blocks, and change them to change the behavior:

if True:

print("true is true")

if False:

print("false is true")

21 of 27

Indentation is important!!

What is wrong with the code below?�

if True:

print("huh?")

IndentationError: expected an indented block

How to fix it? Add tab/spaces before print to indent it

if True:

print("huh yeah")

22 of 27

Exercise

What should this code produce?

if 2 + 2 == 3:print("2 + 2 is 3")

condition = 2 + 2 == 3if condition:print("2 + 2 is 3")

How do we ask if the user likes apples, and print "you like apples" if they respond yes?Remember the input() function?

23 of 27

if true - or else !

else: is something you can put under an if condition:

if the if condition: is not true, the else: will run

What should this code produce?

if 2 + 2 == 3:print("2 + 2 is 3")else:print("2 + 2 is not 3")

24 of 27

Write a choice-adventure game

  • Print out the scenario (Carl the Goblin is trying to get the book o' spells)
  • Print out a choice � ex: "Trolls! sneak or fight or illusion?"
  • Then print if they picked an option, like "fight"
  • Test it out!

25 of 27

Group exercise

_nnnn_

dGGGGMMb

@p~qp~~qMb

M|@||@) M|

@,----.JM|

JS^\__/ qKL

dZP qKRb

dZP qKKb

fZP SMMb

HZM MMMM

FqM MMMM

__| ". |\dS"qML

| `. | `' \Zq

_) \.___.,| .'

\____ )MMMMMP| .'

`-' `--'

Can you draw something by printing strings? ----->

Work together to draw a picture of an animal (or animals)

Copy and paste! Share!

What letters / symbols work best?

How do you do lots of lines?

Starfish? Fish? Bat? Snail? Crab? Worm? Cat? Dog?

26 of 27

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 27

Thanks for coming to class!

Please let us know if you have any questions!