Python Code not working correctly
Solution 1:
The condition
if calculate[2] == opperations:
will compare calculate[2]
with the whole list ["+", "-", "*", "/"]
. You can instead use the in
keyword, to check if a element is inside a sequence:
if calculate[2] in opperations:
# ...
Also, in the part where you compare opperations
with "+"
, "-
... You should be comparing them with calculate[2]
:
if calculate[2] == "+":
A = 1
elif calculate[2] == "-":
A = 2
elif calculate[2] == "*":
A = 3
elif calculate[2] == "/":
A = 4
Notes: Your approach will just handle operations with 1
digit numbers. I would recommend you to use the split(" ")
method so you can use it like:
s = "100 + 20"
parts = s.split(" ") # split by space
parts[0] # 100
parts[1] # +
parts[2] # 20