Amon
Amon

Reputation: 2971

Print string instead of error when list is empty

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

This question is actually a two-parter:

  1. What I'm doing is telling the user to input 'side flip', 'back flip' or 'front flip'. When they do the input is removed from the list 'flips_left'. That works fine until say the user enters the same flip again, I want to return a message telling them they did that already, and to try again.

  2. For some reason I can't get the program to print the else statement when the user inputs something that isn't 'side flip' 'front flip' or 'back flip'. Any ideas? Thanks!

Upvotes: 0

Views: 91

Answers (2)

GWW
GWW

Reputation: 44151

Your if statement logic isn't checking what remains in flips_left

Your best bet would be to do something more generic such as:

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

If you want to keep track of choices already made you could use a dictionary to keep track of which flips have already been performed.

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"

Upvotes: 3

Lambda Fairy
Lambda Fairy

Reputation: 14724

Try adding a print keyword before your message:

else:
    print "That is not a type of flip! Try again!"

Upvotes: 3

Related Questions