Solving Problems With Formulas

Introduction

Welcome back to Performing Operations on Python Data! So far, you have picked up arithmetic operators and built strings with concatenation and f-strings. This represents a well-stocked toolbox, and this lesson is where every tool begins working together on the same task.

Up to now, each lesson has focused on a single skill. Here, we will change gears and combine those skills to solve small, real-world problems from start to finish. Specifically, we will walk through three mini-problems:

  • Calculating a total cost with tax
  • Computing the average of three test scores
  • Splitting a bill evenly among a group of people

Along the way, we will also see what happens when inputs change and how to handle values that arrive as text.

The formula-solving pattern

Almost every small calculation program follows the same three-step recipe:

  1. Store the inputs in variables with clear names.
  2. Compute the result using an arithmetic formula.
  3. Print the result with an f-string so the message reads naturally.

Let's apply this pattern to our first problem: finding the total cost of an order when a tax rate is added to the subtotal.

# Total cost with tax
subtotal = 40.00
tax_rate = 0.08
total = subtotal + subtotal * tax_rate
print(f"Total with tax: ${total}")

We store two inputs, subtotal and tax_rate, then compute total. Notice the formula subtotal + subtotal * tax_rate: thanks to operator precedence, Python evaluates subtotal * tax_rate first (that is the tax amount, 3.2), then adds it to subtotal, yielding 43.2. The f-string then wraps the number into a friendly message:

Total with tax: $43.2

These money examples use float values so we can focus on the arithmetic itself. Real financial applications need more careful handling of money and consistent currency formatting, which we will come back to later.

Controlling order with parentheses in a formula

Another formula: splitting a bill

Let's apply the same three-step pattern to a different situation: a group of friends splitting a restaurant bill evenly. Store the bill, store the number of people, divide, and print the result.

# Split a bill evenly
bill = 87.50
people = 5
each = bill / people
print(f"Each person pays: ${each}")

Here, bill / people gives us the fair share, and the f-string embeds that amount into a message with a dollar sign in front. The output is exactly what we would expect:

Each person pays: $17.5

Notice how the shape of the code is nearly identical to the previous examples. Once you recognize the pattern, new formula problems start to feel familiar.

Changing inputs and re-running

One significant advantage of storing inputs in variables (instead of hard-coding numbers directly inside formulas) is that we can quickly test new scenarios. Suppose we want to see what happens with a larger group and a pricier meal:

bill = 120.00    # was 87.50
people = 8       # was 5
each = bill / people
print(f"Each person pays: ${each}")

We modified only the two input lines; the formula and the print statement remained exactly the same. Running the program now produces:

Each person pays: $15.0

This is the primary reason we use variables: the logic remains constant while the inputs are free to change. We encourage you to tweak these values and re-run the program to build intuition for how each result responds.

Working with text values in formulas

In real-world programs, values do not always start as numbers. A price scraped from a website or read from a file often arrives as a string, such as "19.99". If we multiply a string by a whole number, Python does not raise an error — it repeats the text, so "19.99" * 3 gives "19.9919.9919.99" instead of a total, and nothing warns us. Other numeric operations, such as "19.99" * 1.5 or "19.99" + 5, do raise a TypeError. Either way, we must convert the text to a number before doing math with it.

The solution, as you saw in an earlier course, is to convert the text first using int() or float():

price = "19.99"       # value arrives as text
price = float(price)  # convert text to a float
total = price * 3     # now the math works
print(f"Total for 3 items: ${total}")

We first convert price from a string to a float, then use it in the formula just like any other number. The three-step pattern still applies; we simply insert a conversion step between storing and computing whenever the input is text.

Conclusion and next steps

Nicely done! You have seen how a simple three-step pattern (store inputs, compute with a formula, and print with an f-string) carries us through a wide range of everyday problems: adding tax, averaging scores, and splitting a bill. We also reinforced why parentheses matter when precedence interferes, how changing input variables lets us re-run scenarios instantly, and how float() or int() rescue us when values arrive as text.

Next, a set of hands-on practices awaits you, where you will write these formulas yourself and watch the numbers come to life. Take your time, experiment with different inputs, and let the pattern sink in. Let's roll up our sleeves and start solving!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal