i just finished the course and everything is working fine.
But i noticed that if raw_input is letft empty, the program crash.
"Traceback (most recent call last):
File "python", line 40, in
ValueError: invalid literal for int() with base 10: ''
I want to add a command that, if raw_input == "" (or not int)
returns error message (i was supposed to place it as OR condition under the "thats not even in the ocean" place).
How can i do that?
I looked on stackoverflow and i saw that using the ValueError can help me catching input error, but seems like it's not working...anyone can help?
#import the random function from random import randint #create the game board (5x5) board =  for x in range(5): board.append(["O"] * 5) def print_board(board): for row in board: print " ".join(row) #let's begin to play. 4 turn to guess where is the ship print "Let's play Battleship!" print "You have 4 match to play" #printing the game board print_board(board) #define where the boat is located, using the random function, with len to locate within the game board def random_row(board): return randint(0, len(board) - 1) def random_col(board): return randint(0, len(board) - 1) ship_row = random_row(board) ship_col = random_col(board) #commented line used to show position of battle code #print ship_row #print ship_col #the first turn start for turn in range(4): print "Turn", turn+1 print "%s turn left" % (4-turn) guess_row = int(raw_input("Guess Row:")) guess_col = int(raw_input("Guess Col:")) except ValueError: print "You need to type in a valid integer number!" if guess_row == ship_row and guess_col == ship_col: print "Congratulations! You sunk my battleship!" print "Game Over" break else: if (guess_row < 0 or guess_row > 4) \ or (guess_col < 0 or guess_col > 4) \ or (guess_row=="" or guess_col=="") : #or (not guess_row or not guess_col ): 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) if turn==3: print "Game Over"