首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的battleship.py脚本出现缩进错误

我的battleship.py脚本出现缩进错误
EN

Stack Overflow用户
提问于 2015-05-12 19:24:48
回答 1查看 73关注 0票数 0

我正在尝试创建一个简单的双人游戏,就像经典的战舰一样。因此,我开始学习Python,并且保持简单。我已经创建了一个5x5的网格,我希望玩家(2)能够将一艘船1x1放在棋盘上的任何地方。然后,他们轮流猜测另一个人把船放在哪里。

当我编译我的代码时,我在第61行"else: "上得到一个缩进错误。我知道表示命中和未命中的"H“和"M”将会重叠,因为我将其输出到相同的游戏板。

我想我需要帮助的是代码中的while循环。

代码语言:javascript
复制
import sys

#////////////////////////////Setting up board////////////////////////////////////
board = []

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

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


#///////////////////////////Getting input//////////////////////////////////////////
def user_row():
    get_row = raw_input("Enter ship row between 1 and 5")
    #Not shure if this is the best way of checking that the input is an int
    if int(get_row) == False:
        print "You must enter an integer between 1 and 5"
        get_row = raw_input("Enter ship row...")
        if int(get_row) == False:
            sys.exit()

def user_col():
    get_col = raw_input("Enter ship col between 1 and 5")
    if int(get_col) == False:
        print "You must enter an integer between 1 and 5"
        get_col = raw_input("Enter ship col...")
        if int(get_col) == False:
            sys.exit()

#/////////////////////////Intro//////////////////////////////////////////////////////
print "Let's play Battleship!"
print "This is your ocean"
print_board(board)


#////////////////////////Placing ships//////////////////////////////////////////////
print "Player 1 your up!"
print "Player 2 look away!"
print "Place your ship..."

#Not shure if this will call the two functions chronologic and store them as index 0 and 1 in my array. That is what I want it to do
user1_ship = [user_row(), user_col()]

print_board(board)
print "Player 2 your up!"
print "Player 1 look away!"
print "Place your ship..."

user_2 = [user_row(), user_col()]


#///////////////////////guesswork?//////////////////////////////////////////////////
#Maybe while loops inside while loops is not the best way of running the code over and over until someone sinks the other persons ship
#What Im expecting is the first inside while loop to break the outer loop if the player hits the other players ship otherwise break itself. Likewise with the second inner loop.
while True:
    while True:
        print "Player 1 your turn"
        user1_guess = [user_row(), user_col()]
        if user1_guess == user2_ship:
            board[user1_guess[0]][user1_guess[1]] == "H"
            print "PLAYER 1 WINS!"
    break
        else:
            board[user1_guess[0]][user1_guess[1]] == "M"
            print "You missed"
            break
    while True:
        print "Player 2 your turn"
        user2_guess = [user_row(), user_col()]
        if user2_guess == user1_ship:
            board[user2_guess[0]][user2_guess[1]] == "H"
            print "PLAYER 2 WINS!"
    break
        else:
            board[user2_guess[0]][user2_guess[1]] == "M"
            print "You missed"
            break
EN

回答 1

Stack Overflow用户

发布于 2015-05-12 19:29:18

您的缩进不正确...看看这个:

代码语言:javascript
复制
while True:
    while True:
        print "Player 1 your turn"
        user1_guess = [user_row(), user_col()]
        if user1_guess == user2_ship:
            board[user1_guess[0]][user1_guess[1]] == "H"
            print "PLAYER 1 WINS!"
    break

break语句必须与print语句具有相同的缩进,如下所示:

代码语言:javascript
复制
while True:
    while True:
        print "Player 1 your turn"
        user1_guess = [user_row(), user_col()]
        if user1_guess == user2_ship:
            board[user1_guess[0]][user1_guess[1]] == "H"
            print "PLAYER 1 WINS!"
            break

如果您有时间,请阅读Python Styleguide以提高代码质量。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30189440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档