2

I need to do this action once under certain conditions in a for loop to stop duplicates appearing in the checkboxes. Theoretically, this should work I believe but when the conditions are met for this if statement (cycles == 1 & len(tasks) > 1) it skips over the statement anyway which really confuses me. I have been trying to debug this with breakpoints but I can't figure it out.

tasks = [a, b]

def addToList():
    cycles = 0
    for x in range(len(tasks)):
        cycles += 1
        if cycles == 1 & len(tasks) > 1:
            checkList.destroy()
        checkList = Checkbutton(viewTab,text=tasks[x])
        checkList.pack(anchor='w',side=BOTTOM)
6
  • Without knowing more about listInput and how you're calling addToList, it's hard to say what the issue is or what's causing it. By the way - did you mean and (logical and) instead of & (binary/bitwise and)?
    – Grismar
    Commented May 2 at 0:53
  • len(tasks) > 1 is originally not true as the first time the function is called, the array only has 1 item in it. As the function is called multiple times, cycles is reset to 0 when needed. Despite this, when cycles == 1 and len(tasks) >1 is both true, it skips over the if statement anyway.
    – Moorane
    Commented May 2 at 1:00
  • Please create a minimal reproducible example
    – Julien
    Commented May 2 at 1:07
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented May 2 at 1:13
  • did you mean checklist.destroy()? Commented May 2 at 1:14

1 Answer 1

3

The issue lies in the way you're using the '&' operator. In Python '&' is a bitwise AND operator, not a logical AND operator. For logical AND, you should use 'and'

So, instead of:

if cycles == 1 & len(tasks) > 1:

You should use:

if cycles == 1 and len(tasks) > 1:
1
  • This does in fact work! sorry for the simple oversight, I'm not used to python.
    – Moorane
    Commented May 2 at 1:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.