#Logic from internet. D = dict() output = None with open('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)
e
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 + ')'
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)