[英]Python Validation Check Messing Up My Loop
我正在創建一個Python 2-Player戰艦游戲,除了一些小問題,其他所有工作都差不多完成了。 在玩家將所有飛船放置在棋盤上的階段中-我在驗證是否有重復的飛船時遇到麻煩。 這是我的船舶放置循環代碼:
while True:
for ship_name, ship_size in Game.SHIP_INFO:
# create ship instance
ship1 = Ship(player1, ship_name, ship_size)
# ask user for starting coordinate for ship in form "A1" and split into x,y variables
x, y = ship1.split_coordinates(ship_name,player1.player)
# ask user for ship's position --horizontal or vertical
direction = ship1.ask_ship_location()
# create all coordinates for ship based on size of ship and location
created_coords = ship1.create_ship_coordinates(x, y, ship_size,direction)
# check to see if ship already on board
for coord in created_coords:
if any(coord in ship for ship in grid1.play_one_board):
print("Sorry you already have a ship in that location")
continue
else:
break
# add coordinates to player's grid
grid1.play_one_board.append(created_coords)
# loop through coords for ship to print out on displayed grid
grid1.print_ship_coordinates(created_coords,direction)
這是我剛剛嘗試實施的驗證部分,這會引起問題。
for coord in created_coords:
if any(coord in ship for ship in grid1.play_one_board):
print("Sorry you already have a ship in that location")
continue
else:
break
它可以正確地識別是否已經放置了一個現有坐標-但它會繼續執行循環的下兩個步驟,該步驟將打印電路板,然后繼續進行下一個船舶放置,而無需再次詢問重疊的正確版本船舶布置。 如果船舶重疊有錯誤,只需要找出使循環回到起點的最佳方法即可。 有任何想法嗎? 謝謝。
編輯-根據建議將此代碼更改為此代碼,但未收到任何驗證錯誤。
while True:
for ship_name, ship_size in Game.SHIP_INFO:
# create ship instance
ship1 = Ship(player1, ship_name, ship_size)
ship_exists = True
while ship_exists:
# ask user for starting coordinate for ship in form "A1" and split into x,y variables
x, y = ship1.split_coordinates(ship_name,player1.player)
# ask user for ship's position --horizontal or vertical
direction = ship1.ask_ship_location()
# create all coordinates for ship based on size of ship and location
created_coords = ship1.create_ship_coordinates(x, y, ship_size,direction)
# check to see if ship already on board
for coord in created_coords:
ship_exists = any(coord in ship for ship in grid1.play_board)
if ship_exists:
print("sorry")
else:
break
# function to check for overlapped ships
# ship1.check_overlap(created_coords, grid1.play_one_board)
# add coordinates to player's grid
grid1.play_one_board.append(created_coords)
# loop through coords for ship to print out on displayed grid
grid1.print_ship_coordinates(created_coords, direction)
我相信您的問題在這里:
for coord in created_coords:
if any(coord in ship for ship in grid1.play_one_board):
print("Sorry you already have a ship in that location")
continue
else:
break
如果在現有位置找到了船只,則要繼續請求新的坐標。 在這種情況下,您的continue
實際上將繼續內部循環,而不是外部循環。
這意味着您的循環將檢查所有坐標,並在找到不存在飛船的坐標時中斷,從而導致執行for循環之后的下兩個步驟。 我將添加一個檢查變量,而不只是繼續:
ship_exists = False
for coord in created_coords:
if any(coord in ship for ship in grid1.play_one_board):
print("Sorry you already have a ship in that location")
ship_exists = True
break
if ship_exists:
continue
這將確保(如果已經有船的話)重新執行外循環中的第一步。
=============
最終答案,基於評論
def _are_valid_coordinates(created_coords, play_one_board):
for ship in play_one_board:
for coord in created_coords:
if created_coords in ship:
return False
return True
while True:
for ship_name, ship_size in Game.SHIP_INFO:
# create ship instance
ship1 = Ship(player1, ship_name, ship_size)
valid_coords = False
# ask user for starting coordinate for ship in form "A1" and split into x,y variables
while not valid_coords:
x, y = ship1.split_coordinates(ship_name,player1.player)
# ask user for ship's position --horizontal or vertical
direction = ship1.ask_ship_location()
# create all coordinates for ship based on size of ship and location
created_coords = ship1.create_ship_coordinates(x, y, ship_size,direction)
# check to see if ship already on board
valid_coords = _are_valid_coordinates(created_coords, ship1.play_one_board)
if not valid_coords:
print("Sorry you already have a ship in that location")
else:
break
# add coordinates to player's grid
grid1.play_one_board.append(created_coords)
# loop through coords for ship to print out on displayed grid
grid1.print_ship_coordinates(created_coords,direction)
當您執行continue時 ,它只是從該“ /”繼續到“ create_coords中的coord ”內部循環。
要繼續外循環,您可以基於標志來執行。 類似於以下內容:
already_had_ship = False
for coord in created_coords:
if any(coord in ship for ship in grid1.play_one_board):
already_had_ship = True
print("Sorry you already have a ship in that location")
break
if already_had_ship:
continue
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.