September 12: Branching

Learning Objectives

After today's class, you should be able to:

  • Write if statements to make decisions in a program.
  • Use relational and logical operators in expressions.
  • Implement short functions that solve logic problems.

Lesson Outline

Mini Lecture

Using a debugger

  • Demo: stepping through code with the debugger in Thonny
import math

print("Welcome to the fraction reducer!")

# Input the fraction as two integers
print()
numer = int(input("Numerator? "))
denom = int(input("Denominator? "))
print()

if denom == 0:
    print("Sorry, you can't divide by 0")
    print("Have a nice day")
else:
    # Divide by the greatest common divisor
    gcd = math.gcd(numer, denom)
    numer2 = numer // gcd
    denom2 = denom // gcd

    # Output the result in the right format
    if denom2 == 1:
        print(f"{numer}/{denom} reduces to {numer2}.")
    elif denom2 == denom:
        print(f"{numer}/{denom} is already reduced!")
    else:
        print(f"{numer}/{denom} reduces to {numer2}/{denom2}.")

Class Activity

Your To-Do List

Due Wednesday September 13th at 11pm

  • Submit HW 3 through Canvas and check out the Help page. Remember the attribution statement

Prepare for Quiz 1B

  • Thursday, beginning of class, 25 minutes, in EnGeo 2204.
  • Two parts: concepts and programming (will use lab machine account, canvas and gradescope)
  • Study chapters 1 and 2: both reading and activities
  • Review class activities
  • Review homework programming assignments