mercredi 29 mai 2019

If statement does not work when comparing to integers

I'am trying to make a python sort-of calculator that allows for any amount of digits to be entered and it checks if every second "word" is an operator, but when comparing the length of the sum and the index of the "word" it is currently checking to see if it should print the output, but it does not even though the 2 integers are the same.

operators = ["+", "-", "/", "*"]

def doSum(sum):
    split = sum.split()
    target = len(split)
    if split[1] in operators and "=" not in "".join(split):
        for WORD in split:
            if split.index(WORD) % 2 != 0:
                if WORD in operators:
                    if int(split.index(WORD)) == int(target):
                        print(eval("".join(split)))
                    else:
                        print(target)
                        print(len(split))
                        print("-=-=-=-=-=-=-=-=-=-=-=-")

doSum("1 + 2")
doSum("3 + 3")
doSum("8 - 4")
doSum("1 + 3 + 3 - 1")

The problem lines are lines 10 - 15. I expected the output to be: 3 6 4 6 But I got:

3
3
-=-=-=-=-=-=-=-=-=-=-=-
3
3
-=-=-=-=-=-=-=-=-=-=-=-
3
3
-=-=-=-=-=-=-=-=-=-=-=-
7
7
-=-=-=-=-=-=-=-=-=-=-=-
7
7 
-=-=-=-=-=-=-=-=-=-=-=-
7
7
-=-=-=-=-=-=-=-=-=-=-=-

from the "else block" I use for debugging

Aucun commentaire:

Enregistrer un commentaire