Skip to content

Instantly share code, notes, and snippets.

@pradyungn
Created July 8, 2016 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pradyungn/8ae77602fd35fc1b2ddc41f7a22bc864 to your computer and use it in GitHub Desktop.
Save pradyungn/8ae77602fd35fc1b2ddc41f7a22bc864 to your computer and use it in GitHub Desktop.
https://repl.it/C9t0/0 created by windhowl1
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)
# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
print "Turn", turn + 1
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
row = guess_row - 1
col = guess_col - 1
if row == ship_row and col == ship_col:
print "Congratulations! You sunk my battleship!"
break
else:
if (guess_row < 0 or guess_row > 5) or (guess_col < 0 or guess_col > 5):
print "Oops, that's not even in the ocean."
elif(board[row][col] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
board[row][col] = "X"
print_board(board)
if turn == 3:
print "Game Over"
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux
>>> Let's play Battleship!
Traceback (most recent call last):
File "python", line 13, in <module>
File "python", line 10, in print_board
TypeError: list indices must be integers, not list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment