#Logic from internet.
D = dict()
output = Nonewithopen('var.txt') as v:
for line in v:
k, v = line.strip().split('=')
if k == 'output':
output = v
else:
D[k] = v
for k, v in D.items():
output = output.replace(k, f'({v})')
print(output)

You need to check each entry as you read them and replace the entry with the result of the expression if it contains the keys of existing entries. Thus when you read item e you need to replace "a+b" with the actual values you have already saved earlier.

The current script throws a output as (e+g)+(f+h) but i need all elements of e, g, f h too like the desired output i have mentioned. How to modify this script?

There is no point in copying code from the internet if you do not understand what it is doing. You need to think about how to get the answers you need and write your own code. And if you do it in small steps you can test each step as you go along. The way I would (possibly) do it would be:

for each line of text:
Split the line at the equals sign into key and expression.
Using the expression field find out which operator separates the two values. I used a loop like:
for operator in ['+', '-', '*', '/' ]:
which allows for the basic four.
Once you have found the operator, you can split the expression into:
operand1, operand2, and operator (found in the for loop).
If either operand has a dictionary entry, then replace it with the dictionary entry's value. Thus 'a' would become '2*8'.
You then need to recreate the expression from any changed values thus:
expression = '(' + operand1 + operator + operand2 + ')'

Once you have processed all data then the dictionary entry whose key is 'output' should contain the final expression.

By using the word recursive, I want to express the fact that when a variable is replaced by its formula, you need to reapply the same operation to the formula's variables. I know that 2 nested loops and a boolean will do the trick without resorting to real recursion.

Take a look at the input file. There's is a single operation that requires you to go back and reapply anything. All operations chain together in a linear fashion.

`e`

you need to replace "a+b" with the actual values you have already saved earlier.Once you have processed all data then the dictionary entry whose key is 'output' should contain the final expression.

I know that 2 nested loops and a boolean will do the trick without resorting to real recursion.