Kindly help me figure out the problem with this code


When I put in integer outside the range the code will run and print "Oops,that's not even in the ocean." but still the exercise won't pass.I keep getting an error message:""Make sure you print "Oops, that's not even in the ocean." if the user guesses a row or column that is off the board."

I expect the code to run and move on to next exercise.I'm stuck

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)


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!"
    if guess_row not in range(5) or\
    guess_col not in range(5):
        print "Oops,that's not even in the ocean."
        print "You missed my battleship!"
        print print_board(board)


There should be a space in "Oops,that's not even in the ocean.".


Thanks. I can't believe that space took me ■■■■ to figure out.:relieved:


You're welcome! I make those small reading mistakes all the time too :grin:


I have been trying to run this code using python 3 but I keep getting an error saying "syntax error" then the cursor goes back to the line : print " ".join(b)
What could be the reason for this?


CodeCademy uses python 2, that's mostlikely your problem. But I don't know the exact changes between python 2 and 3.


Alright.Thanks again :smile:


In Python 2.x print is a keyword, but in Python 3.x it's actually a function. So you have to use it with parethesis and give it as a parameter whatever you want to print out.


Thank you for that valuable clarification. Can you also tell me why this line :board.append(grid["O"*5])
comes back as an error saying "TypeError: list indices must be integers or slices, not str" when I run the same code in python 3 yet here in codecademy it runs smoothly? Thanks


Code in your first post works just fine in Python 3 after you put parenthesis and rename raw_input() to input(). Also I'm not sure what are you trying to do with this:

it looks like you're trying to access item in list named "grid" at index "OOOOO" and then append it to the "board"; indexes must be integers


Thanks.I made the changes and the code is running perfectly on python 3. However when I key in the correct values for guess_row and guess_col where I expect the code to print "Congratulations! You sank my battleship" it doesn't do that instead it prints "You missed my battleship" and "Game over".
Since it is a win condition I expect it to run the loop for win only but it doesn't yet it runs that same loop perfectly here on codecademy. What could be the reason for this?


Please post your most recent code, so I can see where's the bug. Btw, your original code works as expected in Python 2 as well as in Python 3.


Here is the code. It runs on well on python 3 but it won't print the "win condition" even after putting the correct integers for guess_col and guess_row. Please check it out for me.Thanks

from random import randint

for i in range(0,5):
def print_board(board):
    for b in board:
        print (" ".join(b))
print ("Let's play battleship")        

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

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


print (ship_col)
print (ship_row)
#Everything from here should go to your loop!
#Be sure to indent four spaces

guess_row= int(input("Guess Row:"))
guess_col= int(input("Guess Col:"))
for win in 'Battleship':
    if guess_row==ship_row and guess_col==ship_col:
        print ("Congratulations! You sank my battleship!")
    if (guess_row<0 or guess_row>4) or (guess_col<0 or guess_col>4):
        print ("Ooops, that's not even in the ocean.")
    elif (board[guess_row][guess_col] =="X"):
        print ("You guessed that one already.")
        print ("You missed my battleship!")
    for turn in range(4):
        print ("Turn",turn+1)
    if turn==3:
        print("Game Over")


You are printing ship_row and ship_col in opposite order, so when you're inputting ship position you're putting col index instead of row index and for col index the other way around.


so I should start with ship_row then ship_col?


That's up to you. I'm just saying you mixed up your ship_row and ship_col when printing them so you're inputting them in wrong order. Check the code I quoted.


I did that and it worked. Thanks
Again why is it that .lower() does not change string to lower case in python 3 yet it does so in python 2?
for example:

pyg= 'sco'
my_name=input("Enter your name:")
if len(my_name)>0 and my_name.isalpha():
    print (my_name)
    print (my_name)
    new_name=my_name+ first + pyg
    print (new_name)
print (empty)

So If I enter something like ROOONEYR it returns the following results.I expect them to be in lower case.What's wrong here?


I'm not sure what you wanted to with new_name or what it should look like, so I don't know if it's what you wanted or not.


I want to print new_name after combining it with first+pyg,It should print the entire new_name in lower case