0

What is the right way to match the second position in my array?

my array (d) from HID is [1,1,0,0,0,0,0] or [1,0,0,0,0,0,0]

when position 2 is 1, I want to meet my condition.

This is what I have hacked together:

    while True:
        d = h.read(64)
        if d:
            for btn, val in enumerate(d):
                if btn == 1:
                    if val == 1:
                        print("Condition Finally Met")
0

1 Answer 1

2

Just index directly into the list:

while True:
    d = h.read(64)
    if len(d) >= 2 and d[1] == 1:
        print("Condition Met")

In Python, lists are zero-indexed (i.e. the first item is numbered 0, not 1), so d[1] gets the value of the second item. We also check that the list actually contains at least two items, just in case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.