Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python codeacademy战舰项目语法错误_Python - Fatal编程技术网

Python codeacademy战舰项目语法错误

Python codeacademy战舰项目语法错误,python,Python,我正在通过codeacademy网站学习python。 现在我;我正试图为战舰项目编写代码,但我遇到了一个问题: 我的代码是: import random board = [] for x in range(0,5): board.append(["O"] * 5) def print_board(board): for row in board: print " ".join(row) print_board(board) def random_row(board):

我正在通过codeacademy网站学习python。 现在我;我正试图为战舰项目编写代码,但我遇到了一个问题: 我的代码是:

import random

board = []

for x in range(0,5):
  board.append(["O"] * 5)

def print_board(board):
  for row in board:
    print " ".join(row)

print_board(board)

def random_row(board):
  return random.randint(0,len(board)-1)

def random_col(board):
    return random.randint(0,len(board[0])-1)

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = input("Guess Row:")
guess_col = input("Guess Col:")

print ship_row
print ship_col

if (guess_row == ship_row and guess_col == ship_col):
    print "Congratulations! You sank my battleship!"
else:
    if((guess_row < 0) or (guess_row > (len(board) -1) or (guess_col < 0) or (guess_col> (len(board) -1)):
        print "Oops, that’s not even in the ocean."
    else:
        print "You missed my battleship!"
        guess_row = "X"
        guess_col = "X"
        print_board(board)
    if board[guess_row][guess_col] == "X":
        print "You guessed that one already."
随机导入
董事会=[]
对于范围(0,5)内的x:
董事会追加([“O”]*5)
def打印板(板):
对于板中的行:
打印“”连接(行)
印刷电路板
def随机_行(板):
返回random.randint(0,len(板)-1)
def随机_柱(板):
返回random.randint(0,len(board[0])-1)
船行=随机船行(板)
船列=随机列(船列)
猜测行=输入(“猜测行:”)
guess\u col=输入(“guess col:”)
打印发货行
打印船型
如果(guess_row==ship_row和guess_col==ship_col):
打印“祝贺你!你击沉了我的战舰!”
其他:
如果((猜测行<0)或(猜测行>(len(board)-1)或(猜测列<0)或(猜测列>(len(board)-1)):
打印“哦,那甚至不在海里。”
其他:
打印“你错过了我的战舰!”
猜一猜
猜一猜
印刷电路板
如果电路板[guess\u row][guess\u col]=“X”:
打印“你已经猜到了。”
但它返回以下错误:

    if((guess_row < 0) or (guess_row > (len(board) -1) or (guess_col < 0) or (guess_col> (len(board) -1)):
                                                                                                         ^
SyntaxError: invalid syntax
如果((猜测行<0)或(猜测行>(len(board)-1)或(猜测列<0)或(猜测列>(len(board)-1)):
^
SyntaxError:无效语法
你能帮我解决这个问题吗

提前感谢各位

您在冒号前面遗漏了一个
(编辑:您也遗漏了另一个)。您的行应该是:

if((guess_row < 0) or (guess_row > (len(board) -1)) or (guess_col < 0) or (guess_col> (len(board) -1))):
如果((猜测行<0)或(猜测行>(len(board)-1))或(猜测列<0)或(猜测列>(len(board)-1)):

您有九个开始圆括号,但只有七个结束圆括号

我个人会删除其中大部分:

 if guess_row < 0 or guess_row >= len(board) or guess_col < 0 or guess_col >= len(board):
应该读一下

    board[guess_row][guess_col] = "X"
另外,对于当前的
猜测行
猜测列
应在将
猜测行
设置为
“X”
之前或之前执行
if board[guess\u row][guess\u col]
检查:

if(guess_row < 0) or (guess_row > (len(board) -1)) or (guess_col < 0) or (guess_col> (len(board) -1)):
如果(猜测行<0)或(猜测行>(len(board)-1)或(猜测列<0)或(猜测列>(len(board)-1)):

谢谢你,现在它已经修复了!而且,我必须像这样制作guess\u row=“X”guess\u col=“X”>>board[guess\u row][guess\u col]=“X”不,我刚刚发现了这个错误并记录在案!旁白:看起来你在混合缩进样式。有时你使用两个空格,有时你使用制表符。[使用
python-tt your_program_name.py
来确认这一点。]这将使您陷入难以调试的麻烦。请使用四个空格选项卡。尝试以下操作:
if((猜测行<0)或(猜测行>(len(board)-1))或(猜测列<0)或(猜测列>(len(board)-1)))
当Python以一种奇怪的方式计算空格时,我想我将不得不使用4个空格的制表符而不是空格。谢谢你的建议!是不是只有我认为列表维度出了问题?当我运行脚本时,它没有将X放在正确的行+列上。。
    board[guess_row][guess_col] = "X"
if(guess_row < 0) or (guess_row > (len(board) -1)) or (guess_col < 0) or (guess_col> (len(board) -1)):