簡體   English   中英

如何在 while 循環中退出 for 循環並返回頂部?

[英]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('---------')
            

*重新編輯,因為我在上一個問題中省略了重要信息

OG問題:

如何退出 for 循環並返回到 while 循環的開頭(即再次要求 new_move 輸入)? 目前,只有當我滿足另外兩個 if 語句時,代碼才會循環,而不是 for 循環中的 if 語句。 它只是直接跳到break,而不是循環回到起點。 我無法成功地將 continue 放在 for 循環中的任何位置。

我通過實施 user212514 的解決方案解決了這個問題。 但是現在我遇到的問題是,當最終的 for 循環完成/滿足時,我沒有退出 while 循環。 我不能在那里放置一個 break 語句而不弄亂其他東西。

您可以在for中使用break

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

修改后的問答

聽起來你會想要使用一個變量來防止在for之后評估if s。

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

您應該將檢查單元是否被占用的循環轉換為 boolean function 以便您可以根據其返回值使用continue ,就像您在主循環的 rest 中所做的那樣。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM