emmartin
emmartin

Reputation: 105

How to fix nested if/for loop

I'm attempting to change a flag to True when a nested dictionary has more than one key. The code currently counts the number of keys correctly, but it is not changing the flag to true.

I've done both the condensed version below and the more broken out version to no avail. I've also rearranged the statement in multiple ways, but cannot get it to trigger.

for page in sd:
        chartcount = len(sd[page])
        print '\n', 'Slide no.', page, '--There is/are', chartcount, 'Chart(s).'
        [combinecheck is True if chartcount > 0 else False]
        print combinecheck

I expect: Slide no. 1 --There is/are 2 Charts. True

I get: Slide no. 1 --There is/are 2 Charts. False

Upvotes: 1

Views: 60

Answers (2)

Dolfa
Dolfa

Reputation: 792

Not sure what are you trying to do with [] part. That looks like list comprehension (that would be useful if you would be creating a list, which you aren't. And you are not assigning that list anywhere either way.), you dont need that. Instead put there:

combinecheck = chartcount > 0

Upvotes: 2

distracted-biologist
distracted-biologist

Reputation: 808

[combinecheck is True if chartcount > 0 else False]

isn't assigning to combinecheck.

Try:

combinecheck = False
if chartcount > 0:
  combinecheck = True

Upvotes: 1

Related Questions