Bad Aim


#1



I need a help.i dont know whats wrong in my code !!
https://www.codecademy.com/courses/python-beginner-en-4XuFm/1/3?curriculum_id=4f89dab3d788890003000096#


Oops, try again. 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


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


#2

How many columns do you have? 5

How many rows do you have? 5 (Correct this.)

You're telling the computer that if the user is not in range from 5 to 5, or 3 to 3, then their input is not in the ocean.

You need to check from 0 to 5...

if guess_row not in range(0,5) or guess_col not in range(0,5):

This checks if their guessed row is not within those 5 rows, or if their guessed column is not in those 5 columns.

If EITHER is true, it tells them they've left the safe bounds of the ocean and entered the vast unknown.


#3

i have changed the line that u have suggested.But again am getting the same error.why??


#4

this is my test:

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


#5

you should indentation> print _board(board)


#6

I think your only mistake is the string you have told the computer to print, which says "Oops,that's not even in the ocean.", instead of "Oops, that's not even in the ocean."

Regarding 825orion's answer, I think range(5) should work as well as range(0,5), since from what I recall, it should give you a list with all the numbers from 0 to 4 all the same.

Hope it helps.


#7

While that's true... It's better to show that range, especially when learning. :slight_smile:


#8

Am getting the error even if i change the string in correct format.what's wrong here?? here is my 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 and guess_col == ship_col:
print "Congratulations! You sank my battleship"
else:
print ("You missed my battleship!")
if guess_row not in range(0,5) or \
guess_col not in range(0,5):
print"Oops,that's not even in the ocean."
else:
board[guess_row][guess_col] = "X"
print_board(board)

Can anyone please reply me!


#9

That's quite odd. I'm literally copying your first post's code, pasting it in my interpreter and changing the string: that's all I'm doing, and it is working for me.


#10

Your latest code seems to bring up the same issue. Make sure you put a space between "Oops," and "that's not even in the ocean". I have double checked the rest, and it just seems perfectly fine to me.


#12

Remember that the index starts from 0 so five is not even in the ocean because i had the same problem as you and this solution worked


#13

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