简体   繁体   English

如何在while循环语句中引用具有布尔项作为值的字典?

[英]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 所有值均为True

And I have this while loop: 我有这个while循环:

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. 我基本上想要当dict中所有值都为false时退出while循环的方法。

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

while any(flips_left.itervalues()):

Here is another way to structure your code that avoids scanning flips_left.itervalues() repeatedly 这是另一种避免重复扫描flips_left.itervalues()代码结构

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!"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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