1

I'm having issues with this code, when i run the code, it always prints "Oops, that's not even in the ocean." no matter what i type. Also if i write letters the code crashes. How can i add a typo check fonction that could solve the crash and make the code resistant from typos from the user. Thank you in advance !

from random import randint

#initializing board + this is my list 
board = []

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

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

#starting the game and printing the board
print "Let's play Battleship!"
print_board(board)

#defining where the ship is
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)

#asking the user for a guess
for turn in range(4):
    guess_row = raw_input("Guess Row:")
    guess_col = raw_input("Guess Col:")

    # if the user's right, the game ends
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        break
    else:
        #warning if the guess is out of the board
        if (guess_row < 0 or guess_row > 5) or (guess_col < 0 or guess_col > 5):
            print "Oops, that's not even in the ocean."

        #warning if the guess was already made
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."

        #if the guess is wrong, mark the point with an X and start again           
        else:
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"

        # Print turn and board again here
        print "Turn " + str(turn+1) + " out of 4." 
        print_board(board)

#if the user have made 4 tries, it's game over
if turn >= 3:
    print "Game Over"
sarah bahs
  • 13
  • 3
  • 2
    Aside: beginners should really be using Python 3 if at all possible. It would have prevented this error by giving you an error message you could have googled. – DSM Oct 15 '15 at 15:39

1 Answers1

2

Your problem lies in the fact that raw_input returns a string, you need to cast it to an integer like this:

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
DJanssens
  • 17,849
  • 7
  • 27
  • 42
  • thank you, and do you know how i can add a typo check fonction ? – sarah bahs Oct 15 '15 at 15:52
  • What do you mean by typo check function? In case you mean type check function, this can be done using the type(object) function, which can be found here: https://docs.python.org/2/library/functions.html#type – DJanssens Oct 15 '15 at 15:55
  • Sorry i wasn't clear enough. If the users types a letter or a space or an enter by mistake the code crashes. i though that maybe my adding a fonction that could prevent the crash, something like this maybe : def typo_check(answer): """verifies if there's a typo""" while answer!='yes' and answer!='no': answer = raw_input("""You aren't answering properly...:""") but i dont know how to make it for my code, thanks in advance – sarah bahs Oct 15 '15 at 16:12
  • What about using `guess_row = get_number(0,4)`, where you define the max value and min value of your row and use a function like: `def get_number(min,max,): guess= raw_input() while guess.isalpha() or (int(guess) < min or int(guess) > max): guess = raw_input("""try entering a number:""") return int(guess)`. You could also combine it for your rows and columns together and check them both in a single function. Is that what you are looking for? – DJanssens Oct 15 '15 at 16:30
  • yes thats what im looking to do, but i dont seem to complete understand how to add it to my code and where exactly ? – sarah bahs Oct 15 '15 at 17:39
  • You can just paste it under your previously created functions `def random_col(board):` and call the function where you initiate your `guess_row` variable. Perhaps consider to take another look at a python tutorial, (especially about functions), because these are really basic issues ;) Goodluck! – DJanssens Oct 15 '15 at 17:59