I'm working on a programming problem where user input can define variables, like so:
length = 1
{'length': '1'}
import math
def main():
curr_formula = input()
substitutions = dict()
while(curr_formula != "0"):
if("=" in curr_formula):
split_formula = curr_formula.split("=")
substitutions[split_formula[0]] = split_formula[1]
curr_split = curr_formula.split(" ")
for i in range(len(curr_split)):
# this if statement never runs for some reason
if(curr_split[i] in substitutions):
curr_split[i] = substitutions[curr_split[i]]
print(''.join(curr_split))
curr_formula = input()
main()
When you split on '='
, there is a space after the first term and before the second.
>>> 'length = 1'.split('=')
['length ', ' 1']
Whereas when you split on ' '
it's removed.
>>> 'length = 1'.split(' ')
['length', '=', '1']
As a result, the keys in your dict are probably different.