Get startedGet started for free

Building a gap analysis between forecasts

Txs Tools now has two forecasts, the original forecast forecast1 and the adjusted forecast forecast2.

The dependencies have already been defined as def dependencies(base_cost_price, base_sales_price, sales_usd), where base_cost_price = 7 and base_sales_price = 15, with forecast2 based off the following adjusted sales unit values:

  • Jul = 700
  • Aug = 220
  • Sep = 520

In this exercise, we will look at how to use a for loop to cycle between two different lists, forecast1 and forecast2 and calculate the difference ("gap") using an incremented index. It is possible to do this simultaneously as both lists have the same length.

This exercise is part of the course

Financial Forecasting in Python

View Course

Exercise instructions

  • Complete the code for the adjustment forecast and original forecast with the required variables.
  • Use an index (setting index to start at 0) to loop through your forecast2 list and print the result of the difference between the forecast2 minus forecast1.
  • Use the .format method as part of the print statement in order to print the result, and increment your index by 1.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Set the two results
forecast1 = ____(7, 15, [700, 350, 650])
forecast2 = dependencies(____, ____, [____, ____, ____])

# Create an index and the gap analysis for the forecast
index = ____
for ____ in forecast2:
    print("The gap between forecasts is {}".____(value - ____[index]))
    ____ += 1
Edit and Run Code