14/19 and 15/19


#1

Here's full code:

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[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print ship_row
print ship_col

Write your code below!

if guess_row == ship_row:
guess_col == ship_col
print "Congratulations! You sank my battleship!"
else:
print "You missed my battleship!"
board[guess_row][guess_col] = 'X'
if guess_row not in range(5) or guess_col not in range(5):
"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!"
print guess_row
print guess_col

I don't quite understand if it works correctly, it never shown 'X' though i passed all exercises, here are some screens:

Imgur

There's one really weird:

Can't find mistake :confused:

later in exercises i have even more weird situations, i think i stopped understanding things some lessons ago :frowning:


#2

Exchange the colon with and. You have to check the column as well. If both conditions / expressions are fullfilled you can enter the if-statements.

At this state X will only be set if you guess the right row.

Therefore change it like this:

if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"

#3

Still i don't understand why I have sth like this

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 6
Guess Col: 7
0
2
You missed my battleship!
Traceback (most recent call last):
File "python", line 34, in
IndexError: list index out of range

instead of message "Oops, that's not even in the ocean.". Also I don't know if it should show the whole field with O's and X's on test run but it doesn't (on game over for example it does, so kinda weird). Also this console message is weird:

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 2
Guess Col: 2
0
3
You missed my battleship!
You guessed that one already.
2
2
None

Could somebody just write perfect, 100% good, working code in every aspect? Would be glad, it will help me as nothing other.


#4

This is 14/9:

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[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print ship_row
print ship_col

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
elif guess_row not in range(5) or \
       guess_col not in range(5):
           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"

This is 15/19:

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):
    # Print (turn + 1) here!
    print "Turn", 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!"
    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)

#5

Thanks.
I went all cuckoo for that.