Flow of Control with logic and data types

by Elliott Hauser

08 Sep 2022

Part I: Intro

Announcements

  • I had to make class remote this week unexpectedly. Apologies!
  • I hope to have about an hour with you before I have to play Dr. Dad

Don't panic

Vocab Palette

What things have you encountered, in the readings or elsewhere, that you’d like to make sure I cover in class today?

Discussion: Logic and the Flow of Control

Boolean values are very simple but very powerful. There are tons of useful ways to construct expressions that evaluate to True or False in Python, and we use these to change the behavior of our program.

Basic if statements act as ‘gates’ to control whether blocks of code get executed. elif and else statements enhance this control.

Some specific concepts to understand:

  • Truithiness: Everything can be evaluated to either true or false. Most things are true.
  • try and except: expecting exceptions (also known as ‘errors’) in your code. This is super helpful for user input. Compare:

…with:

  • Flow of Control: Python does things in a specific order. Sometimes it’ll not execute some code, and any runtime errors in that code won’t appear until or unless it does. Let’s revisit the section on short-circuiting.

Hands-on: Use Conditionals in a Turtle Program

In class, we’re going to add conditionals to a turtle program.

Reading

Your next reading, Chapter 4, will teach you more about conditionals! Feel free to try out some of what you learn in your Logical Turtles trinket, which you’ll bring, and we’ll discuss, next week.

Breakout discussion

Using the Custom Turtle program you brought, discuss your process with your partner. Work on improving your code together.

Afterwards, you may get started on your Logical Turtles program, which you will bring to class.

Elliott Hauser is an Assistant Professor at the UT Austin iSchool. He's hacking education as one of the cofounders of Trinket.io. Find Elliott Hauser on Twitter, Github, and on the web.