Separation of python and calculation depending on the resulting variable

I recently posted a line split question when a character is set up. I have given the following code to calculate the Cartesian coordinates. However, I get an error - "TypeError: object" bool "is not subscription" -. How to fix it?

add_x                    =   "s1"
add_y                    =   "a3"
sample                   =   ("0-0")
coordslst                =   sample.split('-')
user_coordinate_x        =   coordslst[0]
user_coordinate_y        =   coordslst[1]
if    (add_x.split('s'))[0] == ("s"):
    new_coordinate_x    =   str(int(user_coordinate_x) - int((add_x.split('a', 's'))[1]))
elif (add_x[0] == ('a'))[0] == ("a"):
    new_coordinate_x    =   str(int(user_coordinate_x) + int((add_x.split('a', 's'))[1]))
if    (add_y.split('s'))[0] == ("s"):
    new_coordinate_y    =   str(int(user_coordinate_y) - int((add_y.split('a', 's'))[1]))
elif  (add_y.split('a'))[0] == ("a"):
    new_coordinate_y    =   str(int(user_coordinate_y) + int((add_y.split('a', 's'))[1]))
new_coordinates     =   new_coordinate_x + "-" + new_coordinate_y
print new_coordinates
+3
source share
2 answers

As a side note, this line:

coordslst = sample.split('-')

will not work correctly if your data looks like 2--1and -2--1; you need:

sample = '-2--1'
pos = sample.index('-', 1)  # find first '-' after first character
x = sample[:pos]            # up to the dash
y = sample[pos+1:]          # skip the dash
+2
source

Here is this line

elif (add_x[0] == ('a'))[0]:

(add_x[0] == ('a')) returns False

and you try to get the first element to it, for example False[0], which does not make sense.

+4
source

All Articles