简体   繁体   中英

How do I exit a for loop within a while loop and return to the top?

while True:
    new_move = input('Enter the coordinates: ')

    # check if cell is already occupied
    for i, el in enumerate(location):
        if new_move == el and cells[i] != '_':
            print('This cell is occupied! Choose another one!')
            break

    # check if new_move contains non-numerical values
    if not new_move.replace(' ', '').isdigit():
        print('You should enter numbers!')
        continue

    # check if new_move goes beyond 3
    if int(new_move[0]) > 3 or int(new_move[2]) > 3:
        print('Coordinates should be from 1 to 3!')
        continue

    # retrieve index of new_move that matches given coordinate
    for i, el in enumerate(location):
        if new_move == el and cells[i] == '_':
            # replace given index with 'X'
            new_cells = cells[:i] + 'X' + cells[i + 1:]
            # print new board state
            print('---------')
            print(f"| {' '.join(new_cells[0:3])} |")
            print(f"| {' '.join(new_cells[3:6])} |")
            print(f"| {' '.join(new_cells[6:9])} |")
            print('---------')
            

*re-edited because I omitted important info in my previous question

OG question:

How do I exit out of the for loop and return to the start of the while loop (ie ask for new_move input again)? At the moment, the code only loops if I satisfy the two other if statements, but not the if statement within the for loop. It just jumps straight to break and doesn't loop back to the start. I can't successfully place continue anywhere within the for loop.

I solved this by implementing user212514's solution. But now I have the issue that I don't exit the while loop when the final for loop is completed/satisfied. I can't place a break statement in there without messing something else up.

You can use break in the for :

while True:
    new_move = input('Enter the coordinates: ')

    # check if cell is already occupied
    for i, el in enumerate(location):
        if new_move == el and cells[i] != '_':
            print('This cell is occupied! Choose another one!')
            break

    # check if new_move contains non-numerical values
    if not new_move.replace(' ', '').isdigit():
        print('You should enter numbers!')
        continue

    # check if new_move goes beyond 3
    if int(new_move[0]) > 3 or int(new_move[2]) > 3:
        print('Coordinates should be from 1 to 3!')
        continue

Revised question and answer

It sounds like you will want to use a variable to prevent evaluating the if s after the for .

while True:
    new_move = input('Enter the coordinates: ')
    good_coordinates = True
    for i, el in enumerate(location):
        if new_move == el and cells[i] != '_':
            print('This cell is occupied! Choose another one!')
            good_coordinates = False
            break

    if not good_coordinates:
        continue

    # execute your other if statements

You should turn the loop that checks whether the cell is occupied into a boolean function so that you can use continue based on its return value, as you already do in the rest of the main loop.

def is_occupied(position, location):
    for i, el in enumerate(location):
        if position == el and cells[i] != '_':
            return true

    return false

while True:
    new_move = input('Enter the coordinates: ')

    # check if cell is already occupied
    if is_occupied(new_move, location):
        print('This cell is occupied! Choose another one!')
        continue

    # check if new_move contains non-numerical values
    if not new_move.replace(' ', '').isdigit():
        print('You should enter numbers!')
        continue

    # check if new_move goes beyond 3
    if int(new_move[0]) > 3 or int(new_move[2]) > 3:
        print('Coordinates should be from 1 to 3!')
        continue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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