Want to avoid empty input (that made the game crash)


#1

Dear All

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[0]) - 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"

#2

try/except is the way to go, the exception here is indeed a ValueError

execpt is always used in combination with try:

try:
    print "try to do something which might cause an error"
except ValueError:
    print "what to do if you get a value error"

i can't imagine the stackoverflow answer didn't include try


#3

Yes, you are right.

but trying this way, i'm getting a no result anyway

...
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
try:
guess_row==int or guess_col==int
except ValueError:
print "must input something"
...


#4

try/except allows us to catch errors and handle them, so the code which caused the error needs to be in the try section. which is this code:

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

given incorrect input will cause int() to throw an error.


#5

I am very sorry. I guess I did the best I can but if input is emtpy value I am still getting an error: can you help me sorting it out?

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:"))
    try:
        guess_row = int(raw_input("Guess Row:"))
        guess_col = int(raw_input("Guess Col:"))
    except ValueError:
        print "You must input something"

#6

this is good:

    try:
        guess_row = int(raw_input("Guess Row:"))
        guess_col = int(raw_input("Guess Col:"))
    except ValueError:
        print "You must input something"

now you just need to remove the prompts from before the try, given they are still prone to errors


#7

OK!! great! got it!

basically the try have to replace the original "raw input" value.

now working!

thank you man!!!


#8

or just place the original raw inputs inside try/except.

Do you understand why it works now and didn't before?


#9

yes, absolutely. this is very helpful because can be used in many other application!


#10

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