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:

For example, if you paid for 12 cups:

Examples and Use Cases

Let's look at a few more examples:

Common Pitfalls and Best Practices

Common mistakes include:

Best practices:

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:

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: