Conditional statements

In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Conditional statements give us this ability.

age = 20
if age >= 18:
    print('Your age is', age)
    print('adult')

The boolean expression after if is called the condition. If it is true, the indented statement runs. If not, nothing happens.

Alternative execution

age = 3
if age >= 18:
    print('your age is', age)
    print('adult')
else: # don't forget this colon 
    print('your age is', age)
    print('teenager')

Chained conditionals

elif is short for else if. Let's see this example:

age = 3
if age >= 18:
    print('adult')
elif age >= 6:
    print('teenager')
else:
    print('kid')

Q. What will be printed after executing the following code?

age = 20
if age >= 6:
    print('teenager')
elif age >= 18:
    print('adult')
else:
    print('kid')

Nested conditionals

if x == y:
    print('x and y are equal')
else:
    if x < y:
        print('x is less than y')
    else:
        print('x is greater than y')
        
        

Exercise 01

1. Fermat’s Last Theorem says that there are no positive integers a, b, and c such that $a^n + b^n = c^n$ for any values of n greater than 2.

  1. Write a function named check_fermat that takes four parameters — a, b, c and n — and checks to see if Fermat’s theorem holds. If n is greater than 2 and $a^n + b^n = c^n$, the program should print:

    “Holy smokes, Fermat was wrong!”

    Otherwise the program should print:> “No, that doesn’t work.”

  2. Write another function that prompts the user to input values for a, b, c and n, converts them to integers, and uses check_fermat to check whether they violate Fermat’s theorem.

2. Write a function, calculate_bmi that takes two parameters, weight and height, to return BMI value. Write another function, get_bmi_category that prompts user to input values for weight and height, converts them to floats, uses calculate_bmi to calculate BMI value, and returns the corresponding BMI category.

BMI Categories:

  • Underweight = <18.5
  • Normal weight = 18.5–24.9
  • Overweight = 25–29.9
  • Obesity = BMI of 30 or greater

Recursion

It is legal for one function to call another; it is also legal for a function to call itself. It may not be obvious why that is a good thing, but it turns out to be one of the most magical things a program can do. For example, look at the following function:

def countdown(n):
    if n <= 0:
        print('Blastoff!')
    else:
        print(n)
        countdown(n-1)

Q. What happens if we call this function like this

countdown(3)

A function that calls itself is recursive; the process of executing it is called recursion.

As another example, we can write a function that prints a string n times.

def print_n(s, n):
    if n <= 0:
        return
    print(s)
    print_n(s, n-1)

Infinite recursion

def recurse():
    recurse()

If you write encounter an infinite recursion by accident, review your function to confirm that there is a base case that does not make a recursive call. And if there is a base case, check whether you are guaranteed to reach it.

Exercise 02

1. Write a program, factorial.py to compute a factorial of an integer, n.

2. Write a program, fibonacci.py to compute the Fibonacci number of an integer , n.

3. The greatest common divisor of two positive integers is the largest integer that divides each of them without remainder. For example,

gcd(2, 12) = 2

gcd(6, 12) = 6

gcd(9, 12) = 3

gcd(17, 12) = 1

See this website for an example of Euclid's algorithm being used to find the gcd. https://en.wikipedia.org/wiki/Euclidean_algorithm#Worked_example

Write a program, greatest_common_divisor.py to implement this idea recursively. The function gcd() takes in two positive integers and returns one integer.

4. (Optional) Implement recusive algorithm for game: the Tower of Hanoi.

More about the game: https://www.mathsisfun.com/games/towerofhanoi.html

def move(n, source, bridge, destination):
    pass # need to be modified


move(3, 'A', 'B', 'C')
# Expected output:
# A --> C
# A --> B
# C --> B
# A --> C
# B --> A
# B --> C
# A --> C

A visualized version of Tower of Hanoi code can be found here.