Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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无效语法。。。?_Python_Syntax Error - Fatal编程技术网

Python无效语法。。。?

Python无效语法。。。?,python,syntax-error,Python,Syntax Error,我正在努力学习Python和Codecademy,这对我帮助很大。在他们的一节课上,我做了一个简化的战舰游戏。现在,我试图通过添加第二条船来扩展它,但它在“while”函数的“else”周围给出了一个“无效语法”错误。“while”功能的全部含义是在黑板上为第二条船找到一个位置,而这条船还没有被原始船占据。感谢您的帮助 from random import randint #Defines the board board = [] for x in range(5): board.ap

我正在努力学习Python和Codecademy,这对我帮助很大。在他们的一节课上,我做了一个简化的战舰游戏。现在,我试图通过添加第二条船来扩展它,但它在“while”函数的“else”周围给出了一个“无效语法”错误。“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)
来自随机导入randint
#定义董事会
董事会=[]
对于范围(5)内的x:
董事会追加([“O”]*5)
#它应该如何打印电路板
def打印板(板):
对于板中的行:
打印(“.”连接(行))
打印(“让我们玩战舰吧!”)
对于交车范围(1):
打印(“旋转”,旋转+1)
打破
印刷电路板
#在板上的任意位置生成船
def随机_第1行(板):
返回randint(0,len(板)-1)
def随机_col1(电路板):
返回randint(0,len(板[0])-1)
ship1_行=随机第1行(板)
ship1\u col=随机col1(板)
#生成第二条船
def随机_第2行(板):
返回randint(0,len(板)-1)
def随机_col2(电路板):
返回randint(0,len(板[0])-1)
ship2_行=随机_行2(板)
ship2\u col=随机col2(板)
#如果第二艘船已经被第一艘船占据,则为第二艘船找到新位置
#船。当一个未开发的位置被发现时,它将继续游戏。
而ship1_行==ship2_行和ship1_列==ship2_列:
ship2_row=randint(0,长度(板)-1)
ship2_col=randint(0,len(板[0]-1)
其他:
#调试
打印(“发货1行:”,发货1_行)
打印(“装运1列:”,装运1_列)
打印(“”)
打印(“装运2行:”,装运2_行)
打印(“装运2列:”,装运2_列)
对于交车范围(4):
realturn=turn+1
猜测行=int(输入(“猜测行:”)
guess_col=int(输入(“guess column:”)
如果guess\u row==ship\u row和guess\u col==ship\u col:
打印(“祝贺你!你击沉了战舰!”)
打破
其他:
如果(猜测行<0或猜测行>4)或(猜测列<0或猜测列>4):
打印(“哎呀,那个地方根本不在海里!”
elif(董事会[猜测行][猜测列]=“X”):
打印(“你已经猜到那个位置了!”)
其他:
打印(“你错过了战舰!”)
棋盘[猜一排][猜一列]=“X”
如果匝数=3:
打印(“游戏结束!”)
打破
打印(“”)
打印(“旋转”,realturn+1)
印刷电路板

前一行的括号不平衡:

ship2_col = randint(0, len(board[0] - 1)
                   ^open  ^open        ^close

(两个开始和一个结束。)

前一行有不平衡的括号:

ship2_col = randint(0, len(board[0] - 1)
                   ^open  ^open        ^close

(两个开始和一个结束。)

你缺少一个括号:
randint(0,len(board[0]-1))
也给出行号,而不是只在“函数”的“else”周围说“而”你缺少一个括号:
randint(0,len(board[0]-1))
也给出行号,而不是只在“else”周围说“其他”“while”函数的