Python: complete an iteration check on a list before moving on?

353 views Asked by At

So I have this to get input from a user (just learned Python, using 2.7 because I was told to):

def get_move_order():
    global move_order
    move_order=[q for q in raw_input("Enter your move order: ")]
    print "Checking the validity of your move..."
    check_correct_moves_only()

And I have this to make sure that it only has the letters in the moves list:

def check_correct_moves_only():
    moves = ['A','D','S','C','H']
    if all(move_order) in moves:
        return start()
    else:
        print "That's not a proper move!"
        return get_move_order()

Problem is, that doesn't really work for some reason. I originally had it as something like this:

def check_correct_moves_only():
    moves = ['A','D','S','C','H']
        for q in move_order:
            if q not in moves:
                print "That's not a proper move!"
                return get_move_order()
            else:
                return start()

But that will return input of something like AAAAAAR as correct six times (in this case, by printing "Player 1 ready!" six times and "That's not a proper move!" one time. I want it to check all seven (think turn based game, but seven orders given at once by each player) for this error before moving on to my other checks that are in start(), but I can't get the all function to work/maybe I'm using it wrong? I tried using any as well but that didn't work.

2

There are 2 answers

0
Felix On BEST ANSWER

i would suggest the following to you:

def get_move_order():  # Asks for a move order until a valid list of moves was entered
    while True:
        move_order = [q for q in raw_input("Enter your move order: ")]

        print "Checking the validity of your move..."
        if check_correct_moves_only(move_order):
            break  # breaks out of the while loop
        else:
            print "That's not a proper move!"
    # valid move has been entered. Start the game.
    start(move_order)

def check_correct_moves_only(move_order):
    moves = ['A', 'D', 'S', 'C', 'H']
    for q in move_order:
        if q not in moves:
            return False
    return True

You wrote check_correct_moves_only in a recursive way, which is not desirable for your problem. You should also use global variables only when you really need them. Using parameters is more readable in most cases. If you need to use some information in different methods that are called separately, you could also write a class instead.

0
Frank Toner On
def check_correct_moves_only():
    moves = ['A','D','S','C','H']
    for char in move_order:
        # if any of the moves are invalid it returns false
        if char not in moves or len(move_order)<7: # i understand the move must have 7 letters?
            print ("That's not a proper move!")
            return False
        #if all moves are correct it returns true
    return True

def get_move_order():
    global move_order
    # converted input to capitals because your moves list is in caps
    move_order=[q for q in raw_input("Enter your move order: ")].upper()
    print ("Checking the validity of your move...")
    # repeats process if function returns false
    if check_correct_moves_only() == False:  get_move_order()
    # calls start function if function returns true
    else:  start()

this isn't a very pythonic way to do it, you shouldn't even use more than one function, but it works and it's pretty easy to follow.