Don' know what to do anymore


#1

Hey dudes/gales I've been really struggling in the recent chapters, and I don't know what to do, I've been for the last few exercises getting stuck and I've had to check the forum for the answer, and I'm not satisfied with that, I feel like I don't understand why I'm doing x or y, should I restart the whole thing to try and have a better understanding or just keep going one and redo the whole things when I finish ?
btw what's wrong with this code ? It's supposed to remplace the "O" by "X" but it doesn't seem to want to.

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)
print ship_row
print ship_col 
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))


# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed my battleship!"
    board [int(guess_row)] [int(guess_col)] = "X"
    print_board(board)


#2

Hi,
Do you know what the int() fonction is doing ?


#3

I just had a quick look at the code.
Firstly you are receiving the input in the type int .
So no need of converting once again in the below lines.
board [int(guess_row)] [int(guess_col)] = "X"


#4

Yeah it converts the strings given into integers


#5

So dont you think there is something wrong here ? =)

else:
    print "You missed my battleship!"
    board [int(guess_row)] [int(guess_col)] = "X"
    print_board(board)

#6

Ok... So "guess_row" and "guess_col" are already integers so no need to re convert.

Here is what I have, I simply set "X" as new value if he misses the boat... But it doesn't seem to want to change the "O"s to "X"s
(I finished the rest of what was asked too)

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
print "You missed my battleship!"
guess_row = "X"
guess_col = "X"
print_board(board)
if guess_row != range(5) or guess_col != range(5):
print "Oops that's not even in the ocean."
else:
print "You missed my battleship!"


#7

If you wanna change '0' to 'X',
then you need to use the row and column indices of that matrix.

The simple code is :

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
print "You missed my battleship!"
board [guess_row] [guess_col] = "X"
print_board(board)


#8

Thanks a lot for the help :wink:


#9

print "You missed my battleship!"
guess_row = "X"
guess_col = "X"

I assume you want to set the value from "O" to "X" but it's not what you are doing. What you actually do is to set the guess_row and guess_col variables (wich are the variables you use to store the coordinate of the player's guess) the value "X".

What you want to do is to put an "X" in the cell the player tried and you do it with:

board [(guess_row)] [guess_col)] = "X" which can be translated by: set a "X" in the colomn "guess_col" and the row "guess_row".
For example if the player entered: 1-4 it would be:
board[1][4] = "X"
and you have two times [ ], [ ] cause you have a list of lists !
first list the rows and the second is the column !

Hope it makes sense :slight_smile:


#10

Makes sens thanks a lot I was really down I was starting to give up


#11

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