Python 语法无效...?
Python invalid syntax...?
我正在努力学习 Python,Codecademy 帮了我大忙。在他们的一节课中,我制作了一个简化的战舰游戏。现在我试图通过添加第二艘船来扩展它,但是它在 "while" 函数的 "else" 周围给了我一个 "invalid syntax" 错误。 "while" 函数的全部含义是在板上找到第二艘船尚未被原始船占据的位置。感谢所有帮助!
from random import randint
#Defines the board
board = []
for x in range(5):
board.append(["O"] * 5)
#How it should print the board
def print_board(board):
for row in board:
print (" ".join(row))
print ("Let's play Battleship!")
for turn in range(1):
print ("TURN ", turn + 1)
break
print_board(board)
#Spawns the boat on a random location on the board
def random_row1(board):
return randint(0, len(board) - 1)
def random_col1(board):
return randint(0, len(board[0]) - 1)
ship1_row = random_row1(board)
ship1_col = random_col1(board)
#Spawns the 2nd boat
def random_row2(board):
return randint(0, len(board) - 1)
def random_col2(board):
return randint(0, len(board[0]) - 1)
ship2_row = random_row2(board)
ship2_col = random_col2(board)
#Finds a new location for the 2nd boat if it is already taken by the 1st
#boat. It procceds with the game when an untapped location has been found.
while ship1_row == ship2_row and ship1_col == ship2_col:
ship2_row = randint(0, len(board) - 1)
ship2_col = randint(0, len(board[0] - 1)
else:
#Debugging
print ("Ship 1 row: ", ship1_row)
print ("Ship 1 col: ", ship1_col)
print (" ")
print ("Ship 2 row: ", ship2_row)
print ("Ship 2 col: ", ship2_col)
for turn in range(4):
realturn = turn + 1
guess_row = int(input("Guess row: "))
guess_col = int(input("Guess colum: "))
if guess_row == ship_row and guess_col == ship_col:
print ("CONGRATULATIONS! You sunk the battleship!")
break
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print ("Oops, that location isn't even in the ocean!")
elif(board[guess_row][guess_col] == "X"):
print ("You guessed that location already!")
else:
print ("You missed the battleship!")
board[guess_row][guess_col] = "X"
if turn == 3:
print ("GAME OVER!")
break
print (" ")
print ("TURN ", realturn + 1)
print_board(board)
前一行的括号不平衡:
ship2_col = randint(0, len(board[0] - 1)
^open ^open ^close
(二开一关)
我正在努力学习 Python,Codecademy 帮了我大忙。在他们的一节课中,我制作了一个简化的战舰游戏。现在我试图通过添加第二艘船来扩展它,但是它在 "while" 函数的 "else" 周围给了我一个 "invalid syntax" 错误。 "while" 函数的全部含义是在板上找到第二艘船尚未被原始船占据的位置。感谢所有帮助!
from random import randint
#Defines the board
board = []
for x in range(5):
board.append(["O"] * 5)
#How it should print the board
def print_board(board):
for row in board:
print (" ".join(row))
print ("Let's play Battleship!")
for turn in range(1):
print ("TURN ", turn + 1)
break
print_board(board)
#Spawns the boat on a random location on the board
def random_row1(board):
return randint(0, len(board) - 1)
def random_col1(board):
return randint(0, len(board[0]) - 1)
ship1_row = random_row1(board)
ship1_col = random_col1(board)
#Spawns the 2nd boat
def random_row2(board):
return randint(0, len(board) - 1)
def random_col2(board):
return randint(0, len(board[0]) - 1)
ship2_row = random_row2(board)
ship2_col = random_col2(board)
#Finds a new location for the 2nd boat if it is already taken by the 1st
#boat. It procceds with the game when an untapped location has been found.
while ship1_row == ship2_row and ship1_col == ship2_col:
ship2_row = randint(0, len(board) - 1)
ship2_col = randint(0, len(board[0] - 1)
else:
#Debugging
print ("Ship 1 row: ", ship1_row)
print ("Ship 1 col: ", ship1_col)
print (" ")
print ("Ship 2 row: ", ship2_row)
print ("Ship 2 col: ", ship2_col)
for turn in range(4):
realturn = turn + 1
guess_row = int(input("Guess row: "))
guess_col = int(input("Guess colum: "))
if guess_row == ship_row and guess_col == ship_col:
print ("CONGRATULATIONS! You sunk the battleship!")
break
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print ("Oops, that location isn't even in the ocean!")
elif(board[guess_row][guess_col] == "X"):
print ("You guessed that location already!")
else:
print ("You missed the battleship!")
board[guess_row][guess_col] = "X"
if turn == 3:
print ("GAME OVER!")
break
print (" ")
print ("TURN ", realturn + 1)
print_board(board)
前一行的括号不平衡:
ship2_col = randint(0, len(board[0] - 1)
^open ^open ^close
(二开一关)