python - Indent Error with my battleship.py script -
i'm trying create simple 2 player game classic battleship. hence i'm beginning learn python , i'm keeping simple. have created 5x5 grid , want players (2) able place 1 ship 1x1 anywhere on board. take turns guessing other person placed ship.
when compiled code got indent error on line 61 "else: "
. i'm aware "h" , "m" hit , miss overlap since i'm outputting same playing board.
i guess need while loops in code.
import sys #////////////////////////////setting board//////////////////////////////////// board = [] x in range(5): board.append(["o"] * 5) def print_board(board): row in board: print " ".join(row) #///////////////////////////getting input////////////////////////////////////////// def user_row(): get_row = raw_input("enter ship row between 1 , 5") #not shure if best way of checking input int if int(get_row) == false: print "you must enter integer between 1 , 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 , 5") if int(get_col) == false: print "you must enter integer between 1 , 5" get_col = raw_input("enter ship col...") if int(get_col) == false: sys.exit() #/////////////////////////intro////////////////////////////////////////////////////// print "let's play battleship!" print "this ocean" print_board(board) #////////////////////////placing ships////////////////////////////////////////////// print "player 1 up!" print "player 2 away!" print "place ship..." #not shure if call 2 functions chronologic , store them index 0 , 1 in array. want user1_ship = [user_row(), user_col()] print_board(board) print "player 2 up!" print "player 1 away!" print "place ship..." user_2 = [user_row(), user_col()] #///////////////////////guesswork?////////////////////////////////////////////////// #maybe while loops inside while loops not best way of running code on , on until sinks other persons ship #what im expecting first inside while loop break outer loop if player hits other players ship otherwise break itself. likewise second inner loop. while true: while true: print "player 1 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 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
your indentation incorrect... @ this:
while true: while true: print "player 1 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
the break
statement must have same indentation print
statement this:
while true: while true: print "player 1 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
if have time, please read python styleguide improve quality of code.
Comments
Post a Comment