0
votes

I got an array which contains key inputs, and also mouse coordinates inputs.

This array consists of: [W, A, D, X, mouseX, mouseY].

Below I got code which checks if a specific choice have been done; therefore if the user presses W, the array adds [0,1,0,0,mouseX, mouseY].

My question is: How can I get Python to declare that mouseX and mouseY can be any value unless it is an integer, so I can append those values as well?

mouseX = 0
mouseY = 0
for data in train_data:
    img = data[0]
    choice = data[1]
    if choice == [1,0,0,0, mouseX, mouseY]:
        left.append([img,choice])
    elif choice == [0,0,1,0,mouseX, mouseY]:
        right.append([img,choice])
    elif choice == [0,0,0,1,mouseX, mouseY]:
        shoot.append([img,choice])
    elif choice == [0,1,0,0,mouseX, mouseY]:
        forward.append([img,choice])

Thanks!

2
When you declared mouseX and mouseY to 0, they would now accept integer values. You can check this with type(mouseX) which returns int. If you want explicit conversion to integer, without these declarations, use int(mouseX), int(mouseY). - amanb

2 Answers

0
votes

So you really only want to compare the first four items in the list to some reference list. You can do so by:

if choice[0:4] == [1,0,0,0]:
    left.append([img,choice])

etc...

0
votes

In the code as written, the data will never be written unless mouseX and mouseY are equal to 0. To check if neither mouseX or mouseY are integers, and then check for the keypress:

for data in train_data:
    img = data[0]
    choice = data[1]
    mouseX = choice[4]
    mouseY = choice[5]
    if int in [type(mouseX), type(mouseY)]:
        choice = None #comparisons will now fail if integer X or Y
    if choice[:4] == [1,0,0,0]:
        left.append([img,choice])
    elif choice[:4] == [0,0,1,0]:
        right.append([img,choice])
    elif choice[:4] == [0,0,0,1]:
        shoot.append([img,choice])
    elif choice[:4] == [0,1,0,0]:
        forward.append([img,choice])

Note this will fail if data[1] does not have 6 members.