17/19 a real win Plz help


#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)
print ship_row
print ship_col

Everything from here on should go in your for loop!

Be sure to indent four spaces!

turn = 0
for turn in range(4):
print (turn + 1)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

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 "Oops, that's not even in the ocean."
elif(board[guess_row][guess_col] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)
print "Game Over"


#2
#import random for position generation

from random import randint

column headers

board = [[" ","1","2","3","4","5"]]

board with row headers

for x in range(1, 6):
board.append([str(x) +":","O","O","O","O","O"])

format board

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

format_board(board)

choose random location for ship

row between 1 and length of the board - 1

def random_row(board):
return randint(1, len(board)-1)

column between 1 and width of board - 1

def random_col(board):
return randint(1, len(board)-1)

variables for ship location

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

initiate game for four turns

for turn in range(4):

print current turn

print "Turn", turn+1

take a guess

guess_row = int((raw_input("Guess a row (1-5):")))
guess_col = int((raw_input("Guess a column (1-5):")))
if guess_row == ship_row and guess_col == ship_col:
    board[guess_row][guess_col] = "X"
    format_board(board)
    print "Congratulations! You sank my battleship!"
    break
elif board[guess_row][guess_col] == "o":
    format_board(board)
    print "You guessed that one already."
else:
    print "You missed my battleship!"
    if guess_row not in range(6) or guess_col not in range(6):
        format_board(board)
        print "Oops, that's not even in the ocean."
    else:
        board[guess_row][guess_col]="o"
        format_board(board)
    if turn == 3:
        print "Game Over"

#3

from random import randint

board = [[" ","1","2","3","4","5"]]

for x in range(1, 6):
board.append([str(x) +":","O","O","O","O","O"])

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

format_board(board)

def random_row(board):
return randint(1, len(board)-1)

def random_col(board):
return randint(1, len(board)-1)

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

for turn in range(4):

print "Turn", turn+1

guess_row = int((raw_input("Guess a row (1-5):")))
guess_col = int((raw_input("Guess a column (1-5):")))
if guess_row == ship_row and guess_col == ship_col:
    board[guess_row][guess_col] = "X"
    format_board(board)
    print "Congratulations! You sank my battleship!"
    break
elif board[guess_row][guess_col] == "o":
    format_board(board)
    print "You guessed that one already."
else:
    print "You missed my battleship!"
    if guess_row not in range(6) or guess_col not in range(6):
        format_board(board)
        print "Oops, that's not even in the ocean."
    else:
        board[guess_row][guess_col]="o"
        format_board(board)
    if turn == 3:
        print "Game Over"

#4

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)
print ship_row
print ship_col

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))

    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 "Oops, that's not even in the ocean."
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
        else:  
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
        # Print (turn + 1) here!
        print "Turn", turn + 1
        print_board(board)
        print
        if turn == 3:
            print "Game Over"

#5

Or you can try using the While loop like this

from random import randint

board = []

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

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

print_board(board)

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

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

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

turn = 4
while(turn > 0):
guess_row = int(raw_input("Guess a Row: "))
guess_col = int(raw_input("Guess a Column: " ))

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulation you have sunk the battleship!"
    board[ship_row][ship_col] = "B"
    print_board(board)
    break    
else:
    if(guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
        print "Oops, that's not even in the coean."
        print_board(board)
        turn -= 1
        print "You have " + str(turn) + " turn(s) left"
        
    elif board[guess_row][guess_col] == "X":
        print"You've already guessed that one already."
        turn -= 1
        print "You have " + str(turn) + " turn(s) left"
    else:
        print "You missed my battleship!"
        board[guess_row][guess_col] = "X"
        print_board(board)
        turn -= 1
        print "You have " + str(turn) + " turn(s) left"

    if turn == 0:
        print "You LOSE!"
        board[ship_row][ship_col] = "B"
        print_board(board)
        break

#6

This was done so the turn would be counting down instead of counting up also i added the fact that you can see where the Battleship was located at either when you win or lose