Exercise: Free Coffee Cups


For each of the 6 coffee cups I buy, I get a 7th cup free. In total, I get 7 cups.

Knowing that I paid for cups cups, compute and print the total number of cups I would get (number of cups I paid for + number of cups I got for free).


Example:

For example, if cups was 12, the answer would be 14.

Why? Because I paid for 12 cups and I got 2 for free, which is a total of 14 cups.

Introduction

In this exercise, we will solve a problem related to a promotional offer where for every 6 coffee cups purchased, you get an additional cup for free. This is a common scenario in many loyalty programs and understanding how to calculate the total number of items received can be very useful in various programming tasks.

Understanding the Basics

The fundamental concept here is to determine how many free cups you get based on the number of cups you paid for. For every 6 cups bought, you get 1 free cup. This means if you buy 12 cups, you get 2 free cups, and so on. The formula to calculate the total number of cups is:

total_cups = paid_cups + free_cups

Where free_cups can be calculated as paid_cups // 6.

Main Concepts

Let's break down the problem:

  • Calculate the number of free cups using integer division.
  • Add the number of free cups to the number of paid cups to get the total number of cups.

For example, if you paid for 12 cups:

  • Number of free cups = 12 // 6 = 2
  • Total number of cups = 12 + 2 = 14

Examples and Use Cases

Let's look at a few more examples:

  • If you paid for 6 cups, you get 1 free cup, so total = 6 + 1 = 7
  • If you paid for 15 cups, you get 2 free cups, so total = 15 + 2 = 17
  • If you paid for 20 cups, you get 3 free cups, so total = 20 + 3 = 23

Common Pitfalls and Best Practices

Common mistakes include:

  • Not using integer division to calculate the number of free cups.
  • Forgetting to add the free cups to the paid cups to get the total.

Best practices:

  • Use clear variable names to make the code readable.
  • Comment your code to explain the logic.

Advanced Techniques

For more complex scenarios, such as varying promotional offers, you can use functions to encapsulate the logic and make the code reusable. For example, you could create a function that takes the number of cups and the promotion details as parameters.

Code Implementation

Here is the Python code to solve the problem:

def total_cups(paid_cups):
    # Calculate the number of free cups
    free_cups = paid_cups // 6
    # Calculate the total number of cups
    total = paid_cups + free_cups
    return total

# Example usage
cups = 12
print(total_cups(cups))  # Output: 14

Debugging and Testing

To debug and test the function, you can use various test cases:

def test_total_cups():
    assert total_cups(6) == 7
    assert total_cups(12) == 14
    assert total_cups(15) == 17
    assert total_cups(20) == 23
    print("All tests passed!")

test_total_cups()

Thinking and Problem-Solving Tips

When approaching this problem:

  • Break down the problem into smaller parts.
  • Think about the mathematical operations needed.
  • Write pseudocode before implementing the actual code.

Conclusion

In this lesson, we learned how to calculate the total number of coffee cups received based on a promotional offer. This involved understanding integer division and basic arithmetic operations. Mastering these concepts is crucial for solving similar problems in programming.

Additional Resources

For further practice, consider the following resources: