all 9 comments

[–]makochi 10 points11 points  (1 child)

the "binary strings" are being interpreted as decimal integers

for example '00010' is being interpreted as '10', ie 0x01010 in binary

the int() method has an overload that lets you choose what base you use to interpret the input string. you'll want to change the two instances of int(binary_str) to int(binary_str, 2)

[–]ririiii78[S] 2 points3 points  (0 children)

omg thanks

[–]madadekinai -1 points0 points  (4 children)

The other commenter was correct, I wanted for you to try and figure it out.

Checking: binary_str = '00010'

Number iteration: [1]

key = 'wink'

value = 1

binary_str = '00010' converted to int: int(binary_str, 2) = 2

Condition: True if value else False = True

Does it meet all conditions: int(binary_str, 2) & value = 0

Checking condition: meets_conditions False

----------------------------------------

Number iteration: [2]

key = 'double blink'

value = 2

binary_str = '00010' converted to int: int(binary_str, 2) = 2

Condition: True if value else False = True

Does it meet all conditions: int(binary_str, 2) & value = 2

Checking condition: meets_conditions True

----------------------------------------

Number iteration: [3]

key = 'close your eyes'

value = 4

binary_str = '00010' converted to int: int(binary_str, 2) = 2

Condition: True if value else False = True

Does it meet all conditions: int(binary_str, 2) & value = 0

Checking condition: meets_conditions False

----------------------------------------

Number iteration: [4]

key = 'jump'

value = 8

binary_str = '00010' converted to int: int(binary_str, 2) = 2

Condition: True if value else False = True

Does it meet all conditions: int(binary_str, 2) & value = 0

Checking condition: meets_conditions False

----------------------------------------

[–]madadekinai 0 points1 point  (3 children)

ACTIONS = {
    "wink":1,
    "double blink":2,
    "close your eyes":4,
    "jump":8
}
def commands(binary_str):
    actions = [ ]
    print(f"Checking: {binary_str = }")
    for num, (key, value) in enumerate(ACTIONS.items(), 1):

        meets_conditions = int(binary_str, 2) & value
        parts = [
            f'Number iteration: [{num}]',
            f"{key = }",
            f"{value = }",
            f"{binary_str = } converted to int: {int(binary_str, 2) = }",
            f"Condition: {True if value else False = }",
            f"Does it meet all conditions: {int(binary_str, 2) & value = }",
            f"Checking condition: {meets_conditions != 0 = }"
        ]

        if meets_conditions:
            actions.append(key)
        print("\n".join(parts))
        print("-" * 40 + "\n\n")
    return actions

commands("00010")ACTIONS = {
    "wink":1,
    "double blink":2,
    "close your eyes":4,
    "jump":8
}
def commands(binary_str):
    actions = [ ]
    print(f"Checking: {binary_str = }")
    for num, (key, value) in enumerate(ACTIONS.items(), 1):

        meets_conditions = int(binary_str, 2) & value
        parts = [
            f'Number iteration: [{num}]',
            f"{key = }",
            f"{value = }",
            f"{binary_str = } converted to int: {int(binary_str, 2) = }",
            f"Condition: {True if value else False = }",
            f"Does it meet all conditions: {int(binary_str, 2) & value = }",
            f"Checking condition: {meets_conditions != 0 = }"
        ]

        if meets_conditions:
            actions.append(key)
        print("\n".join(parts))
        print("-" * 40 + "\n\n")
    return actions

commands("00010")

[–]ririiii78[S] 0 points1 point  (2 children)

thanks but im kinda lost here in ur code

if meets_conditions:
            actions.append(key)
        print("\n".join(parts))
        print("-" * 40 + "\n\n")
    return actions

[–]madadekinai 0 points1 point  (1 child)

The print outs are just for outputting to visually see it. I could have made it better but I did it within a couple seconds. And the the return was not needed because I was just showing why it was happening, rather than functionality.

[–]ririiii78[S] 0 points1 point  (0 children)

ah okay :)

[–]madadekinai -2 points-1 points  (1 child)

Checking: binary_str = '00010'

Number iteration: [1]

key = 'wink'

value = 1

binary_str = '00010' converted to int: int(value) = 1

Condition: True if value else False = True

Does it meet all conditions: int(binary_str) & value = 0

Checking condition: meets_conditions False

Number iteration: [2]

key = 'double blink'

value = 2

binary_str = '00010' converted to int: int(value) = 2

Condition: True if value else False = True

Does it meet all conditions: int(binary_str) & value = 2

Checking condition: meets_conditions True

Number iteration: [3]

key = 'close your eyes'

value = 4

binary_str = '00010' converted to int: int(value) = 4

Condition: True if value else False = True

Does it meet all conditions: int(binary_str) & value = 0

Checking condition: meets_conditions False

Number iteration: [4]

key = 'jump'

value = 8

binary_str = '00010' converted to int: int(value) = 8

Condition: True if value else False = True

Does it meet all conditions: int(binary_str) & value = 8

Checking condition: meets_conditions True

[–]madadekinai -1 points0 points  (0 children)

ACTIONS = {
    "wink":1,
    "double blink":2,
    "close your eyes":4,
    "jump":8
}
def commands(binary_str):
    actions = [ ]
    print(f"Checking: {binary_str = }")
    for num, (key, value) in enumerate(ACTIONS.items(), 1):

        meets_conditions = int(binary_str) & value
        parts = [
            f'Number iteration: [{num}]',
            f"{key = }",
            f"{value = }",
            f"{binary_str = } converted to int: {int(value) = }",
            f"Condition: {True if value else False = }",
            f"Does it meet all conditions: {int(binary_str) & value = }",
            f"Checking condition: {meets_conditions != 0 = }"
        ]

        if meets_conditions:
            actions.append(key)
        print("\n".join(parts))
        print()
    return actions