6. Conditionals
Learning conditional statements and recursive functions in Python.
- Conditional statements
- Alternative execution
- Chained conditionals
- Nested conditionals
- Recursion
- Infinite recursion
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.
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')
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')
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')
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.
-
Write a function named
check_fermat
that takes four parameters —a
,b
,c
andn
— 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.”
- Write another function that prompts the user to input values for
a
,b
,c
andn
, converts them to integers, and usescheck_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
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)
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.
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.