Python - Control Flow Challenges

April 1, 2021 pexels-pixabay-35888.jpg Vuong Huynh

Noted from Learn Python 3 | Codecademy

Source Code

1. Large Power

For the first code challenge, we are going to create a method that tests whether the result of taking the power of one number to another number provides an answer which is greater than 5000. We will use a conditional statement to return True if the result is greater than 5000 or return False if it is not. In order to accomplish this, we will need the following steps:

  1. Define the function to accept two input parameters called base and exponent
  2. Calculate the result of base to the power of exponent
  3. Use an if statement to test if the result is greater than 5000. If it is then return True. Otherwise, return False
# 1. Large Power
# Create a function named large_power() that takes two parameters named base and exponent.
# If base raised to the exponent is greater than 5000, return True, otherwise return False
def large_power(base, exponent):
    return base**exponent > 5000

print(large_power(2, 13))
# should print True
print(large_power(2, 12))
# should print False

2. Over Budget

Let’s say we are trying to save some money and we are watching our budget. We need to make sure that the result of our spending is less than the total amount we have allocated for each of the categories. Our function will accept a parameter called budget which describes our spending limit. The next four parameters describe what we are spending our money on. We need to sum all of our spendings and compare it to the budget. If we have gone over budget, we will return True. Otherwise we return False. Here are the steps we need:

  1. Define the function to accept five parameters starting with budget then food_bill, electricity_bill, internet_bill, and rent
  2. Calculate the sum of the last four parameters
  3. Use if and else statements to test if the budget is less than the sum of the calculated sum from the previous step.
  4. If the condition is true, return True otherwise return False
# 2. Over Budget
# Create a function called over_budget that has five parameters named 
# budget, food_bill, electricity_bill, internet_bill, and rent.
# The function should return True if budget is less than the sum of the other four parameters — 
# you’ve gone over budget! Return False otherwise.
def over_budget(budget, food_bill, electricity_bill, internet_bill, rent):
    return budget < (food_bill + electricity_bill + internet_bill + rent)

print(over_budget(100, 20, 30, 10, 40))
# should print False
print(over_budget(80, 20, 30, 10, 30))
# should print True

3. Twice As Large

In this challenge, we will determine if one number is twice as large as another number. To do this, we will compare the first number with two times the second number. Here are the steps:

  1. Define our function with two inputs num1 and num2
  2. Multiply the second input by 2
  3. Use an if statement to compare the result of the last calculation with the first input
  4. If num1 is greater then return True otherwise return False
# 3. Twice As Large
# Create a function named twice_as_large() that has two parameters named num1 and num2.
# Return True if num1 is more than double num2. Return False otherwise.
def twice_as_large(num1, num2):
    return num1 > num2*2

print(twice_as_large(10, 5))
# should print False
print(twice_as_large(11, 5))
# should print True

4. Divisible By Ten

To make things a bit more challenging, we are going to create a function that determines whether or not a number is divisible by ten. A number is divisible by ten if the remainder of the number divided by 10 is 0. Using this, we can complete this function in a few steps:

  1. Define the function header to accept one input num
  2. Calculate the remainder of the input divided by 10 (use modulus)
  3. Use an if statement to check if the remainder was 0. If the remainder was 0, return True, otherwise, return False
# 4. Divisible By Ten
# Create a function called divisible_by_ten() that has one parameter named num.
# The function should return True if num is divisible by 10, and False otherwise. Consider using modulo (%) to check for divisibility.
def divisible_by_ten(num):
    return num % 10 == 0

print(divisible_by_ten(20))
# should print True
print(divisible_by_ten(25))
# should print False

5. Not Sum To Ten

Finally, we are going to check if the summation of two inputs does not equal ten. Our function will accept two inputs and add them together. If the two numbers added together are not equal to ten, then we will return True, otherwise, we will return False. Here is what we need to do:

  1. Define the function to accept two parameters, num1 and num2
  2. Add the two parameters together
  3. Test if the result is not equal to 10
  4. If the sum is not equal, return True, otherwise, return False
# 5. Not Equal
# Create a function named not_sum_to_ten() that has two parameters named num1 and num2.
# Return True if num1 and num2 do not sum to 10. Return False otherwise.
def not_sum_to_ten(num1, num2):
    return num1 + num2 != 10

print(not_sum_to_ten(9, -1))
# should print True
print(not_sum_to_ten(9, 1))
# should print False
print(not_sum_to_ten(5,5))
# should print False

6. In Range

Let’s start the advanced challenge problems by testing if a number falls within a certain range. We will accept three parameters where the first parameter is the number we are testing, the second parameter is the lower bound and the third parameter is the upper bound of our range. These are the steps required:

  1. Define the function to accept three numbers as parameters
  2. Test if the number is greater than or equal to the lower bound and less than or equal to the upper bound
  3. If this is true, return True, otherwise, return False
# 6. In Range
# Create a function named in_range() that has three parameters named num, lower, and upper.
# The function should return True if num is greater than or equal to lower and less than or equal to upper. Otherwise, return False.
def in_range(num, lower, upper):
    return lower <= num <= upper

print(in_range(10, 10, 10))
# should print True
print(in_range(5, 10, 20))
# should print False

7. Same Name

We need to write a program that checks different names and determines if they are equal. We need to accept two strings and compare them. Here are the steps:

  1. Define the function to accept two strings, your_name and my_name
  2. Test if the two strings are equal
  3. Return True if they are equal, otherwise return False
# 7. Same Name
# Create a function named same_name() that has two parameters named your_name and my_name.
# If our names are identical, return True. Otherwise, return False.
def same_name(your_name, my_name):
    return your_name == my_name

print(same_name("Colby", "Colby"))
# should print True
print(same_name("Tina", "Amber"))
# should print False

8. Always False

There are some situations that you normally want to avoid when programming using conditional statements. One example is a contradiction. This occurs when your condition will always be false no matter what value you pass into it. Let’s create an example of a function that contains a contradiction. It will contain a few steps:

  1. Define the function to accept a single parameter called num
  2. Use a combination of <, > and and to create a contradiction in an if statement.
  3. If the condition is true, return True, otherwise return False. The trick here is that because we’ve written a contradiction, the condition should never be true, so we should expect to always return False.
# 8. Always False
# Create a function named always_false() that has one parameter named num.
# Using an if statement, your variable num, and the operators >, and <, make it so your function will return False no matter what number is stored in num.
# An if statement that is always false is called a contradiction. You will rarely want to do this while programming, but it is important to realize it is possible to do this.
def always_false(num):
    if num > 0 and num < 0:
       return True
    return False

print(always_false(0))
# should print False
print(always_false(-1))
# should print False
print(always_false(1))
# should print False

9. Movie Review

We want to create a function that will help us rate movies. Our function will split the ratings into different ranges and tell the user how the movie was based on the movie’s rating. Here are the steps needed:

  1. Define our function to accept a single number called rating
  2. If the rating is equal to or less than 5, return "Avoid at all costs!"
  3. If the rating was less than 9, return "This one was fun."
  4. If neither of the if statement conditions were met, return "Outstanding!"
# 9. Movie Review
# Create a function named movie_review() that has one parameter named rating.
# If rating is less than or equal to 5, return "Avoid at all costs!". If rating is between 5 and 9, return "This one was fun.". If rating is 9 or above, return "Outstanding!"
def movie_review(rating):
    if rating <= 5:
        return "Avoid at all costs!"
    elif 5 < rating < 9:
        return "This one was fun."
    return "Outstanding!"

print(movie_review(9))
# should print "Outstanding!"
print(movie_review(4))
# should print "Avoid at all costs!"
print(movie_review(6))
# should print "This one was fun."

10. Max Number

For the final challenge, we are going to select which number from three input values is the greatest using conditional statements. To do this, we need to check the different combinations of values to see which number is greater than the other two. Here is what we need to do:

  1. Define a function that has three input parameters, num1, num2, and num3
  2. Test if num1 is greater than the other two numbers
    • If so, return num1
  1. Test if num2 is greater than the other two numbers
    • If so, return num2
  1. Test if num3 is greater than the other two numbers
    • If so, return num3
  1. If there was a tie between the two largest numbers, then return "It's a tie!"
# 10. Max Number
# Create a function called max_num() that has three parameters named num1, num2, and num3.
# The function should return the largest of these three numbers. If any of two numbers tie as the largest, you should return "It's a tie!".
def max_num(num1, num2, num3):
    if num1 > num2 and num1 > num3:
        return num1
    elif num2 > num1 and num2 > num3:
        return num2
    elif num3 > num1 and num3 > num2:
        return num3
    else:
        return "It's a tie!"
            
print(max_num(-10, 0, 10))
# should print 10
print(max_num(-10, 5, -30))
# should print 5
print(max_num(-5, -10, -10))
# should print -5
print(max_num(2, 3, 3))
# should print "It's a tie!"