Home > other >  Dictionary making weird key:value pairs
Dictionary making weird key:value pairs

Time:04-23

I want to make a simple calculator, where you just type in something like "2 2*2" and it calculates the answer. First step was to find, and cut out the individual numbers, which I tried doing below.

print("Put an equals sign at the end to finish the equation.")
equation = input("Calculate: ")
symbols = [" ","-","*","/","="]
x = 0
for symbols in equation:
    subequation = {f"N{x}" : equation[0:equation.find(symbols)]}
    x = x   1
print(subequation)

But when I input:

Calculate: 2 2*2=

I don't get the expected output of:

{'N0':'2 ', 'N1':'2*','N2':'2='}

But rather:

{'N5': '2 2*2'}

I'm still very new to python, so please also tell me if there's anything I can do better. Thanks.

CodePudding user response:

There are a lot of small changes to get the result you added to your question, so I will go through each one.

I've created an empty dictionary to store your sub equation in: subequation = {}

Your original for loop of for symbols in equation: would overwrite your list of symbols, I believe what you are trying to do is go through each of the symbols and check if they are in the equation, which you can do with a for loop and then an if statement as below:

for symbol in symbols:
    if symbol in equation:

As mentioned by Jhanzaib Humayun in the comments, you're overwriting your subequation in each loop, rather than adding, so they have provided the code to add to a dict as below:

subequation[f"N{x}"]= equation[0:equation.find(symbols)]

But equation[0:equation.find(symbols)] will always start from the begining of the equation and return everything up to (and not including the symbol), so would return:

{'N0': '2', 'N1': '2 2', 'N2': '2 2*2'}

Which is not what you want, so I changed this to:

equation[equation.find(symbol)-1:equation.find(symbol) 1]

Which will now only include the number and symbol, but as Titouan L commented, this won't work for numbers with multiple digits, so I have come up with a last_symbol variable which will work backwards from the symbol it's looking at to find the last one, and keep anything in between. I have done this as below:

last_symbol = max([i for i in range(equation.find(symbol)-1, 0, -1)
                   if equation[i] in symbols]   [-1])
subequation[f"N{x}"]= equation[last_symbol 1:equation.find(symbol) 1]

This works backwards from the symbol to find the indexes of all previous symbols ([i for i in range(equation.find(symbol)-1, 0, -1) if equation[i] in symbols]) and finds the maximum of this (i.e. the most recent). I added [-1] to this list as for the first symbol, there will not be a previous symbol, and when adding to the dictionary, I have done equation[last_symbol 1:equation.find(symbol) 1], so for the first symbol, this will go from 0:equation.find(symbol) 1

The full code is:

print("Put an equals sign at the end to finish the equation.")
equation = input("Calculate: ")
symbols = [" ","-","*","/","="]
subequation = {}
x = 0
for symbol in symbols:
    if symbol in equation:

        last_symbol = max([i for i in range(equation.find(symbol)-1, 0, -1)
                           if equation[i] in symbols]   [-1])
        subequation[f"N{x}"]= equation[last_symbol 1:equation.find(symbol) 1]
        x = x   1
print(subequation)

Which returns the below as desired:

{'N0': '2 ', 'N1': '2*', 'N2': '2='}

CodePudding user response:

As some comments mentionned, you overwrite your variable subequation each loop. On top of that, your variable symbols is overwritten by the loop variable.

In order to get the same output as you requested, I changed the for loop to iterate over each character and check if it match a symbol of the list symbols.

print("Put an equals sign at the end to finish the equation.")
equation = input("Calculate: ")
symbols = [" ", "-", "*", "/", "="]
subequation = {}
x = 0
for i in range(len(equation)):
    if equation[i] in symbols:
        subequation[f"N{x}"] = equation[x:i 1]
        x = i 1

print(subequation)

The output is the following :

{'N0': '2 ', 'N2': '2*', 'N4': '2='}

CodePudding user response:

As Emi OB commented, you overwrite symbols, as well as subequation, in your for loop.

I think you mean something like this:

print("Put an equals sign at the end to finish the equation.")
equation = input("Calculate: ")
symbols = [" ","-","*","/","="]
subequation = {f"N{x 1}" : equation[0:equation.find(symbols[x])] for x in range(len(symbols))}
print(subequation)
  • Related