简体   繁体   中英

How can I reference a dictionary with boolean terms as values in a while-loop statement?

I have this dictionary:

flips_left = {'front flip': True,
              'side flip': True,
              'back flip': True
}

All the values are True

And I have this while loop:

while flips_left[flip_choice] == True:
            flip_choice = raw_input("Do a flip ")
            if flip_choice in flips_left:
                if flips_left[flip_choice]:
                    print flip_lines[flip_choice]
                    flips_left[flip_choice] = False
                else:
                    print "You already did a %s" % flip_choice
            else:
                print "That is not a type of flip"

        print "Great! You completed the WOD!"

I basically want a way for the while loop to exit when all the values in the dict are false.

any()测试传递的iterable的每个元素的真实性。

while any(flips_left.itervalues()):

Here is another way to structure your code that avoids scanning flips_left.itervalues() repeatedly

for flip in flips_left:
    while True:
        flip_choice = raw_input("Do a flip ")
        if flip_choice in flips_left:
            if flips_left[flip_choice]:
                print flip_lines[flip_choice]
                flips_left[flip_choice] = False
                break
            else:
                print "You already did a %s" % flip_choice
        else:
            print "That is not a type of flip"

print "Great! You completed the WOD!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM