0
def flips():
        print "Alright the workout of the day is a front-flip, back-flip and
                       side flip"                       
        flips_left = ['side flip', 'front flip', 'back flip']
        flip_choice = None

        while len(flips_left) > 0:
            flip_choice = raw_input("Do a flip ")
            if "side flip" in flip_choice:
                 print flip_lines['side_flip']
                 flips_left.remove('side flip')
                 print "Great! Now just do the rest: %s" % flips_left
             elif "front flip" in flip_choice:
                 print flip_lines['front_flip']
                 flips_left.remove('front flip')
                 print "Good stuff! This is what you have left: %s" %
                                 flips_left
             elif "back flip" in flip_choice:
                 print flip_lines['back_flip']
                 flips_left.remove('back flip')
             else:
              "That is not a type of flip! Try again!"

         print "Great! You completed the WOD!"

这个问题实际上是一个两部分:

  1. 我正在做的是告诉用户输入“侧空翻”、“后空翻”或“前空翻”。当他们这样做时,输入将从列表'flips_left'中删除。这工作正常,直到说用户再次输入相同的翻转,我想返回一条消息告诉他们他们已经这样做了,然后再试一次。

  2. 由于某种原因,当用户输入不是“侧翻”、“前翻”或“后翻”的内容时,我无法让程序打印 else 语句。有任何想法吗?谢谢!

4

2 回答 2

3

您的 if 语句逻辑没有检查剩余的内容flips_left

您最好的选择是做一些更通用的事情,例如:

if flip_choice in flips_left:
    print flip_lines[flip_choice]
    flips_left.remove(flip_choice)
else:
    print "Some error"

如果您想跟踪已经做出的选择,您可以使用字典来跟踪已经执行了哪些翻转。

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

...

if flip_choice in flips_left:
    if flips_left[flip_choice]:
        print flip_lines[flip_choice]
        flips_left[flip_choice] = False
    else:
        print "You have already done %s!" % flip_choice
else:
    print "That is not a valid flip"
于 2013-10-01T03:23:17.090 回答
3

尝试print在消息前添加关键字:

else:
    print "That is not a type of flip! Try again!"
于 2013-10-01T03:24:49.293 回答