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!"
这个问题实际上是两个部分:
我正在做的是告诉用户输入'侧翻','后翻'或'前翻'。当他们这样做时,输入将从列表'flips_left'中删除。这样工作正常,直到说用户再次进入相同的翻转,我想返回一条消息,告诉他们他们已经这样做了,并再试一次。
由于某些原因,当用户输入的内容不是“侧翻”,“正面翻转”或“后翻”时,我无法让程序打印else语句。有任何想法吗?谢谢!
答案 0 :(得分: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"
答案 1 :(得分:3)
尝试在邮件前添加print
关键字:
else:
print "That is not a type of flip! Try again!"