Trying to run battleship program in actual python program


#1



from random import randint

board = []

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

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

print "Let's play Battleship!"
print_board(board)

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

ship_row = random_row(board)
ship_col = random_col(board)

for turn in range(4):
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Column:"))

    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        break
    else:
        if (guess_row <0 or guess_row >4) or (guess_col < 0 or guess_col >4):
            print "That's not even in the ocean!"
        elif(board[guess_row][guess_col] == "X":
             print "You've already guess that!"
        else:
             print "You missed my battleship!"
        if turn == 3:
             print "Game Over"
    print "Turn", turn +1
    print_board(board)

Not running and has multiple errors and will not run. Any help would be great


#2

what are the errors? Which version of python are you using?


#3

man do i learn in this site a specific version of python or what?


#4

well yes, you run a specific python version.


#5

what is this version?


#6

it is 2.7.3, not sure about the last digit. But the last digit always has really small changes


#7

is it the most used?


#8

and do i have to learn another version of python to program on another versions?


#9

python2 and python3 are not compatible. There is some difference in syntax (print with brackets) and more

If you write a new app, you should use python3. If you learn python2, there is just a small step to python3. Sometimes you have library's which only exist in python2, or you want your program to work for both 2 and 3.


#10

okay thanks i got now i intend to learn both thanks i got a lot to do


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.